Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utcs!mnetor!seismo!cmcl2!philabs!mcnc!rti-sel!dg_rtp!throopw From: throopw@dg_rtp.UUCP Newsgroups: net.lang.c Subject: Re: Sizeof structure members Message-ID: <444@dg_rtp.UUCP> Date: Wed, 16-Jul-86 13:35:52 EDT Article-I.D.: dg_rtp.444 Posted: Wed Jul 16 13:35:52 1986 Date-Received: Fri, 18-Jul-86 00:59:32 EDT References: <148@danews.UUCP> Lines: 27 Summary: a working solution is presented and may even be legal > lvc@danews.UUCP (Larry Cipriani) > [How does one take sizeof a struct member without an instance of it?] > I didn't want to define a global variable of type 'struct utmp *', > because I didn't need it, and lint would complain because I didn't > use it. I could declare an external variable of type 'struct utmp *', > that I'd never use, but this seems just as bad as the unused global. 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? -- A program without a loop and a structured variable isn't worth writing. --- Alan J. Perlis -- Wayne Throop !mcnc!rti-sel!dg_rtp!throopw