Sizeof structure members

Larry Cipriani lvc at danews.UUCP
Wed Jul 30 06:13:35 AEST 1986


> > [How does one take sizeof a struct member without an instance of it?]
> > ....

> This solution is fairly portable, and may even be legal:
> 
>         struct s {char a[3];};
>         void main(){
>             printf( "%d\n", sizeof( ((struct s *)0)->a ) );
>         }
> 
> It works on a at least a couple of machines, and lint doesn't complain.
> It may be argued that it is not legal, since it is dereferencing the
> null pointer.  On the other hand, sizeof is guaranteed not to evaluate
> the object being sizeofed, and to only calculate resulting types, not
> values.  It's issues like these that give language lawyers headaches,
> right?
> 
> Wayne Throop      <the-known-world>!mcnc!rti-sel!dg_rtp!throopw

It works on my machines Sys 5 Rel 2 3B[2/5/20]s.  I like this very much!
Thanks a lot.  I would have said thanks a week ago but danews is down more
than it is up.
-- 

Larry Cipriani		AT&T Network Systems
danews!lvc		"Nothing is worse than an itch you can never scratch."



More information about the Comp.lang.c mailing list