Will this trick work in C?

后端 未结 5 962
终归单人心
终归单人心 2021-01-21 15:50

I want to add a field to a structure in C. So for example I have the following structure.

struct A
{
 some_type x;
 some_type y;
}

I declare a

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-21 16:46

    No it won't work. It would work if you change it a bit:

    struct A
    {
     some_type x;
     some_type y;
    }; /* <- note semicolon here */
    
    
    struct B
    {
     struct A a;
     some_type z;
    }; /* ... and here */
    
    
    int some_function(struct A *a ); /* ... and here ... */
    
    
    struct B *b;
    ......
    struct A *a = (struct A*)b;
    some_function( a );
    

提交回复
热议问题