You can cast (pointers to) structs in C quite easily:
For example there is struct A:
typedef struct {
int a;
int b;
}A;
and a struct B:
typedef struct {
A parent;
int c;
}B;
Now if x
is a pointer to a struct B, I could do the following:
A *a_struct = (A*) x;
Now I am asking if this is actual defined behaviour or if it is just 'random' that it works with (some) compilers (I tested it with gcc
)