Re: C Offsetof
- From: mihirtr@xxxxxxxxx
- Date: Fri, 31 Aug 2007 16:24:04 -0000
On Aug 31, 11:08 am, "Malcolm McLean" <regniz...@xxxxxxxxxxxxxx>
wrote:
<mihi...@xxxxxxxxx> wrote in message
news:1188575456.336130.313000@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi,
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));
Are you sure you get a pointer to mem, and not the value of mem itself?
Ie a union Member **, not a union Member * ?
--
Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm- Hide quoted text -
- Show quoted text -
I am getting following:
Buffer received_buff;
void * my_ptr;
my_ptr = (void *)received_buff.mem;
So I have my_ptr, from here I need to get "id".
Hope I am clear.
.
- References:
- C Offsetof
- From: mihirtr
- Re: C Offsetof
- From: Malcolm McLean
- C Offsetof
- Prev by Date: Re: Macro redefinition
- Next by Date: Re: pointer conversion
- Previous by thread: Re: C Offsetof
- Next by thread: Macro redefinition
- Index(es):
Relevant Pages
|