Question on accessing structure



All,
See the below piece of C code snippet


typedef struct _a
{
int ia;
int ja;
}A;


typedef struct _b
{
int ib;
int jb;
}B;


typedef struct _x
{
A a;
B b;

}X;


void func(X *ptr)
{
ptr->b.ib = 10;
ptr->b.jb = 20;
ptr->a.ia = 48;
ptr->a.ja = 50;

}

main()
{
A *temp = (A*)malloc(sizeof(A));
func(temp);
printf("%d\n",((X*)temp)->b.ib);

}

why does not the above program result in segmentation error.
I am just allocating memory for structure A and typecasting it to
structure X and writing value to its members i.e Structure A and
structure B.
I compiled above program with MS VC++ and gcc ...it just gives me
warning and not runtime error as well.....and o/p is right.....

Output is
10


.



Relevant Pages