Compatible types and structures in C

后端 未结 5 1064
深忆病人
深忆病人 2020-12-14 02:18

I have the following code:

int main(void)
{
    struct { int x; } a, b;
    struct { int x; } c;
    struct { int x; } *p;

    b = a;   /* OK */
    c = a;          


        
5条回答
  •  攒了一身酷
    2020-12-14 03:19

    Looking at the draft specification I'm guessing you're relying on the conditions that come after the statement:

    Moreover, two structure, union, or enumerated types declared in separate translation units are compatible if their tags and members satisfy the following requirements ...

    I think that the fact that these are all decared in the same C file means that they are in a single translation unit.

    At a guess it would seem that this guarantees that when two C files include a header that declares a type then instances of that type will be compatible.

提交回复
热议问题