pete said:
/* BEGIN new.c */
#include <stdio.h>
#include <stddef.h>
int main(void)
{
struct emp {
int a;
char b;
int c;
};
printf("offsetof(struct emp, a) is %lu\n",
(long unsigned) offsetof(struct emp, a));
printf("offsetof(struct emp, b) is %lu\n",
(long unsigned) offsetof(struct emp, b));
printf("offsetof(struct emp, c) is %lu\n",
(long unsigned) offsetof(struct emp, c));
return 0;
}
/* END new.c */
$ gcc -Wall -Wextra p2.c -o out
$ ./out
offsetof(struct emp, a) is 0
offsetof(struct emp, b) is 4
offsetof(struct emp, c) is 8
$
In this case, my implementation's macro is essentially the same as
Plauger's.
Linux:
#ifdef __compiler_offsetof
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
Plauger:
#define offsetof(T, member) ((_Sizet)&((T *)0)->member)
Can you say a few words about how it works?