[net.lang.c] Sizeof structure members

lvc@danews.UUCP (07/13/86)

I recently wrote a program that made use of the utmp structure.
I wanted to define a global character array of the same size as
a member (ut_line) of utmp.  Since the size is wired in as 12
and not #defined (which is what I would have done!), I decided to
define the array as:

char ftty[sizeof getutent()->ut_line];

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.

Without getutent() (which is what could happen in similar situations)
I believe I am stuck with either of the above 'solutions'.  Does anyone
know if I'm wrong about this ?  I use Sys 5 Rel 2 Unix.

Thanks,
-- 

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

throopw@dg_rtp.UUCP (07/16/86)

> 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      <the-known-world>!mcnc!rti-sel!dg_rtp!throopw

lvc@danews.UUCP (Larry Cipriani) (07/29/86)

> > [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."