sizeof a struc field

Doug Gwyn gwyn at smoke.BRL.MIL
Sun Sep 17 10:06:17 AEST 1989


In article <7710 at microsoft.UUCP> peterwu at microsoft.UUCP (Peter Wu ) writes:
>What's the simplest way to obtain the sizeof a field inside a struc
>without declaring a variable?
>struct abc {
>  int def;
>  char ghi;
>  long jkl;
>};
>I know this works:
>#define SIZEGHI sizeof(((struct abc *)0)->ghi)

No, that doesn't necessarily work.  Review the interminable discussions
about use of null pointers.

#define	SIZEGHI	(sizeof(char))

works.  There's no way to do this using the member name but not its
type, without using some presumed object.  However, since the object
need not exist for sizeof, you could try

#define SIZEGHI (sizeof ((struct abc *)malloc(sizeof(struct abc)))->ghi)



More information about the Comp.lang.c mailing list