2017-02-11 73 views
8

Verilen bu yapılar:C'deki bu sendikaların kullanımı geçerli/uyumlu mu?

typedef struct { 
    //[...] 
} StructA; 

typedef struct { 
    StructA a; 
    //[...] 
} StructB; 

typedef union { 
    StructA a; 
    StructB b; 
} Union; 

eşdeğer değil tanımsız altında iki erişim yöntemleri musunuz?

Union u; 
memcpy(&u.b, /*...*/); //Pretend I populated StructB here 
u.a; // Method 1 
u.b.a; // Method 2 

StructA'nın StructB'nin ilk üyesi olduğunu unutmayın.

Bunu bir kod tablosunda buldum, yalnızca standart olup olmadığını veya herhangi bir hizalama kümesi olup olmadığını merak ediyorum.

cevap

5
typedef union { 
    StructA a; 
    StructB b; 
} Union; 

a birliğe b aynı ofsete sahip olan:

yılında ofset 0 etmiştir.

Aramalar eşdeğerdir.