Re: C Offsetof



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.

.



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)