sizeof a struc field

Pearlman stuart at dtix.dt.navy.mil
Wed Sep 20 00:03:00 AEST 1989


In article <11086 at smoke.BRL.MIL> gwyn at smoke.BRL.MIL (Doug Gwyn) writes:
: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.

I don't see what is wrong with Peter's method.  Sizeof gets evaluated
at compile time. In some instances, the address of its argument may
not yet have been fixed by the compiler when it is evaluated.  What is
important is the type of sizeof's argument, and that certainly seems
well defined.  Or am I totally confused?
				Stuart Pearlman	<stuart at dtix.dt.navy.mil>
				Hadron, Inc. 
				9990 Lee Hway.
				Fairfax, VA 22030  (703) 359-6100



More information about the Comp.lang.c mailing list