int main()
{
// forward declaration
struct myStruct_st *mS; // Note that this will expand as "struct struct myStruct_st *mS wh开发者_运维问答ich does not make any sense to me"
return 0;
}
// definition of myStruct_s
typedef struct myStruct_s
{
int x;
int y;
} myStruct_st;
I understand that myStruct_s is the structure that needs to be forward declared. I had this typo in my code which seemed to compile. I wonder how though. Does anyone know?
The local struct has nothing to do with the struct defined outside of main()
. In main()
you (forward-)declare a struct, define a pointer to that struct and never define the struct. That's perfectly OK. It so happens that you define a struct with the same name outside main()
.
I think you misunderstand how the typedef
works -- it is not macro substitution.
In particular, using struct myStruct_s
after the typedef
is not the same as "struct struct myStruct_s
" -- it's simply struct myStruct_s
, as it reads on the face. The typedef introduces a token which can be used rather than struct ...
, but it doesn't expand like a macro and it doesn't "wipe out" the struct ...
declaration, which can still be used.
精彩评论