0

Is there any problem with the code .
Why this code is generating all offsets as 28.

#include<stdlib.h>

#define offsetof( T , M ) (( size_t )(( char *)&(( T *)0) ->r - \
                          (char *)(T *) 0))

int main(void)
{
        struct STU {
        char name[20];
        struct DATE {
                short day ; short month ; short year ;
        }D;

        int r ;
        };
        
        size_t pos =  offsetof( struct STU , name[0] );
        printf(" pos name[0] = %d ", pos );
        
        pos =  offsetof( struct STU , r );
        printf(" pos r = %d ", pos );

         pos =  offsetof( struct STU , D.month );
         printf(" pos month = %d ", pos );

         pos =  offsetof( struct STU , D.year );
         printf(" pos year = %d ", pos );
return 0;
}
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Gaiety
0

Did you mean to put M instead of r in your macro?

Yes U are Right.

i Modified.

#define offsetof( T , M ) (( size_t )(( char *)&(( T *)0) ->M - (char *)(T *) 0))

Thanks.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.