cstruct

Why does this C code compile? C struct typdef


I wrote the following program:

typedef struct blahblah {
    int x;
    int y;
} Coordinate;

int main () {
   Coordinate p1;
   p1.x = 1;
   p1.y = 2;

   //blah blah has not been declared as a struct, so why is it letting me do this?
   struct blahblah p2;
   p2.x = 5;
   p2.y = 6; 
}

Can anyone explain to me what's going on?


Solution

  • You said:

    blah blah has not been declared as a struct,

    Actually, it has:

    typedef struct blahblah {
        int x;
        int y;
    } Coordinate; 
    

    This is both a typedef Coordinate, and a definition of struct blahblah. What the definition says is: