Question 6
Back to Exercise
The program below has a bug. Which statement correctly describes it?
#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!