Thursday, April 9, 2009

The first line of a source file contains the following line.The compiler warns about "struct x declared inside parameter list". What is the compiler worried about?

extern int f(struct x *);

For two structures to be compatible, they must not only have the same tag name but be defined in the same scope. A function prototype, however, introduces a new, nested scope for its parameters. Therefore, the structure tag x is defined in this narrow scope, which almost immediately disappears. No other struct x pointer in this translation unit can therefore be compatible with f's first parameter, so it will be impossible to call f correctly (at least, without drawing more warnings). The warning alluded to in the question is trying to tell you that you shouldn't mention struct tags for the first time in function prototypes.

(The warning message in the question is actually produced by gcc, and the message runs on for two more lines, explaining that the scope of the structure declared "is only this definition or declaration, which is probably not what you want.")

No comments: