> As per my knowledge union members share the same place of memory
By the time you've taking into padding and alignment, the amount of overlap between any two apparently adjacent variables might be zero.
Which is seems to be in this case.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int main()
{
union u {
struct s {
int a;
int b;
} n;
struct ss {
int c;
long d;
} ni;
} uu;
uu.ni.c = 1;
uu.ni.d = 0;
printf("%d %d\n", (uu.n.a), (uu.n.b));
printf("n.a is at %ld\nn.b is at %ld\nni.d is at %ld\n",
offsetof(union u,n.a), offsetof(union u,n.b), offsetof(union u,ni.d) );
return 0;
}
$ ./a.out
1 32767
n.a is at 0
n.b is at 4
ni.d is at 8
The assignment of ni.d completely misses all the bytes where n.b would reside, so all you see is random garbage.
The a and c members, by virtue of being the first member in their respective structs, AND of the same type, can be freely assigned in one and inspected in the other with no problem at all.
But this is a unique special case.