Having the following C code:
struct Point2_s;
struct Point1_s{
int x;
int y;
Point2_s P2;
} Point1;
struct Point2_s{
int x;
int y;
} ;
int main() {
...
return 0;
}
I'm getting an error:
unknown type name ‘Point2_s’
Can anyone can please explain me WHY it doesn't work? Why doesn't the struct Point2_s declaration is insufficient when defining the Point1_s member P2?
In this line
there is declared an incomplete structure specifier
struct Point2_s.In this declaration
there is used an unknown name
Point2_s. It is not the same asstruct Point2_s.But even if you will write
nevertheless you may not use an incomplete type in a data member declaration.
From the C Standard (6.7.2.1 Structure and union specifiers)
Instead you need to write
Or you could write
In this case the name
Point2_sis an alias for the type specifierstruct Point2_s.On the other hand, as it is pointed out in other answers you may use pointers to incomplete types because pointers themselves are always complete types. That is you may write