C Offsetof



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));

.



Relevant Pages

  • Re: string compare
    ... short int length; ... char data; ... I want to create string processing functions like strcmp, ...
    (comp.lang.c)
  • Re: conversion to non-scalar type requested error
    ... int val; ... a char array to a struct. ...
    (comp.lang.c)
  • Re: string compare
    ... I am using custom string structures in a project. ... short int length; ... char data; ...
    (comp.lang.c)
  • [EXPL] AppleFileServer (AFS) FPLoginExt DoS
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... typedef struct {char PATH;} PATH_T; ... int connect_afp ...
    (Securiteam)
  • Re: string compare
    ... I am using custom string structures in a project. ... short int length; ... char data; ...
    (comp.lang.c)