Is this type punning well defined?

  • A+

I have structure like below.

struct result{     int a;     int b;     int c;     int d; } 

and union like below.

union convert{    int arr[4];    struct result res; } 

and I type pun as below.

  int arr1[4] = {1,2,3,5};   union convert *pointer = (union convert *) arr1; // Here is my question, is it well defined?    printf("%d %d/n", pointer->res.a, pointer->res.b); 


pointer->res.a is fine but the behaviour of pointer->res.b is undefined.

There could be an arbitrary amount of padding between the a and b members.

Some compilers allow you to specify that there is no padding between members but of course then you are giving up portability.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: