Re: C Offsetof
- From: "Malcolm McLean" <regniztar@xxxxxxxxxxxxxx>
- Date: Fri, 31 Aug 2007 17:08:33 +0100
<mihirtr@xxxxxxxxx> wrote in message news:1188575456.336130.313000@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,Are you sure you get a pointer to mem, and not the value of mem itself?
I have following structures and union
typedef struct
{
int a[100];
int b;
char *c;
}TEST_1;
typedef struct
{
int a[100];
char c[100];
}TEST_2;
union Member
{
TEST_1 test_1;
TEST_2 test_2;
};
typedef enum{
START_ID_0 = 0x2000,
START_ID_1,
START_ID_2,
START_ID_3,
START_ID_4
}SomeIdd;
typedef struct BufferTag
{
SomeId *id;
union Member *mem;
}Buffer;
In my program somehow I get pointer to "mem" in struct "Buffer". Now I
want to get value stored in "id". Is there a way. I read at multiple
places that I can use offsetof but when I try it gives by Segmentation
fault. Can you experts check this and let me know.
FOllowing is what I am doing.
#define GetMemFromBuf (out,source) \
(out) = (Buffer *) \
((char *) (source) - (offsetof(Buffer, mem)))
Following is that way I am accessing it.
Buffer *myBuff;
GetMemFromBuf (myBuff, (void *)myMem);
printf("ID = %d",(*myBuff->id));
Ie a union Member **, not a union Member * ?
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
.
- Follow-Ups:
- Re: C Offsetof
- From: mihirtr
- Re: C Offsetof
- References:
- C Offsetof
- From: mihirtr
- C Offsetof
- Prev by Date: Re: size_t problems
- Next by Date: Re: Macro redefinition
- Previous by thread: C Offsetof
- Next by thread: Re: C Offsetof
- Index(es):
Relevant Pages
|