I have two struct types
typedef struct {
int i;
int j;
} struct_a;
typedef struct {
int i;
} struct_b;
where struct_a
and struct_b
obviously share a common initial sequence. Based on these structs I have the following snippet in which I convert between struct_a
and struct_b
by casting and dereferencing pointers
struct_a a = { 0, 1 };
printf("%i %i\n", a.i, a.j);
struct_b b = *((struct_b *)&a);
printf("%i\n", b.i);
struct_a c = *((struct_a *)&b);
printf("%i %i\n", c.i, c.j);
The output of the above code after compiling with gcc is
0 1
0
0 0 <= ?
My question is why does c
lose the value of j
that was specified when initializing a
? Is it because dereferencing the pointers, for instance, in
struct_b b = *((struct_b *)&a);
effectively copies the struct?