----- Original Message ----- 
From: "Ganesh" 
To: 
Sent: Monday, May 03, 2004 7:45 PM
Subject: Hi

> Hi Vijay,
> How to get the base address of the structure if I know the address
> of one member of the structure;
> I have one structure as
> 
> Struct P {
>     int *x;
>     float *y;
>     char *z;
>     int *a;
>     int *temp;
>     struct P *next;
>     int hello, hi;
>     float I, j, k;
> } *ptr;
> 
> I know ptr->next. Is it possible to get the base address of the structure. I
> don't know the size of the Structure.
> 
> Rgds,
> Ganesh
> 
    Yes, it is possible to get the base address of the structure.  You will
need three kinds of information, viz, 

    +   pointer to the structure object
    +   type of the structure
    +   one member of the structure (which you have)

    You can write a macro like this:

        #define struct_base(ptr, type, member)  \
                (type*)((char*) ptr - (char*)(&((type*)0)->member))


    An example is:

    #include 
    
    int main ( void )
    { 
        struct P p = { 0, };

        printf ( "base: %p\nmember: %p\n", 
                    (void*) struct_base ( &p, struct P, ptr ),
                    (void*) &p -> ptr );

        return EXIT_SUCCESS;
    }

    Source: geocities.com/vijoeyz/faq/c

               ( geocities.com/vijoeyz/faq)                   ( geocities.com/vijoeyz)