Afficher un message
Vieux 31/01/2008, 12h52   #1
Sid
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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


  Réponse avec citation
 
Page generated in 0,04611 seconds with 9 queries