LearnFrenzy
Sign in Take a free test

Typedef · Find the Error · Question 6

Error: declaring a variable of an incomplete (opaque) type Answer, explanation & discussion

  • Find the error
  • Hard
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

The program below has a bug. Which statement correctly describes it?

main.c
#include <stdio.h>

typedef struct secret Secret;      /* struct secret is defined in another file */

int main(void) {
    Secret s;
    printf("%zu\n", sizeof s);
    return 0;
}
  • A typedef cannot refer to an undefined struct
  • B struct secret is incomplete here, so its size is unknown – only pointers (Secret *) can be declared Correct
  • C sizeof cannot be used on typedefs
  • D There is no bug
Correct Answer: Option B

This is the opaque-type pattern: outside the defining file you may only use Secret * and the API functions. Declaring an object, using sizeof or accessing members needs the full definition.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links