Question on accessing structure
- From: Sid <Sid4Comp@xxxxxxxxx>
- Date: Thu, 31 Jan 2008 03:52:45 -0800 (PST)
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
.
- Follow-Ups:
- Re: Question on accessing structure
- From: Default User
- Re: Question on accessing structure
- From: Richard Tobin
- Re: Question on accessing structure
- From: vippstar
- Re: Question on accessing structure
- Prev by Date: Re: A solution for the allocation failures problem
- Next by Date: Re: Question on accessing structure
- Previous by thread: Quick check on signed promotion of bytes
- Next by thread: Re: Question on accessing structure
- Index(es):
Relevant Pages
|