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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.