Hello varja11!
If you want to print the address of a variable it is better tu use '%p' instead of 'd'
All three variables of int have the same size, so they starts at the same place in memory.
If you would change your code like this:
Code:
#include <stdio.h>
union UnionName
{
int x;
int y;
int z;
unsigned char parts[4];
}u1;
int main(int argc, char **argv)
{
u1.x = 30;
u1.y = 20;
u1.z = 10;
printf ("\n x : %d ", u1.x);
printf ("\n y : %d ", u1.y);
printf ("\n z : %d ", u1.z);
printf ("\n Location of x : %p ", &u1.x);
printf ("\n Location of y : %p ", &u1.y);
printf ("\n Location of z : %p\n", &u1.z);
printf ("\n parts[0] : %d ", u1.parts[0]);
printf ("\n parts[1] : %d ", u1.parts[1]);
printf ("\n parts[2] : %d ", u1.parts[2]);
printf ("\n parts[3] : %d\n", u1.parts[3]);
printf ("\n Location of parts[0] : %p ", &u1.parts[0]);
printf ("\n Location of parts[1] : %p ", &u1.parts[1]);
printf ("\n Location of parts[2] : %p ", &u1.parts[2]);
printf ("\n Location of parts[3] : %p ", &u1.parts[3]);
printf ("\n size of union : %lu\n", sizeof(u1));
return 0;
}
You will have different address for u1.parts[]);