sizeof a struc field

Joe English jeenglis at nunki.usc.edu
Sat Oct 14 10:03:21 AEST 1989


davidsen at crdos1.UUCP (bill davidsen) writes:
>  Better yet, can someone post a method to get the size of a field
>without having to create the struct or union which *is*
>	a. portable
>	b. readable
>
>  Why discuss the virtues of NULL and why it should be special in this
>case, assume that even if it works people will flame you for using it,
>and let someone prove how smart they are by posting a solution to the
>problem.


struct foo {
	...
	sometype field;
	...
};

Use 'sizeof(sometype)' instead of 'sizeof(((struct foo *)0)->field)'

See how smart I am? :-)

Personally, I prefer 'sizeof(type)' over 'sizeof(object)'
as a general rule anyway.  This is not so good a solution
when there is information hiding going on -- you know that
struct foo has a field named 'field,' but its type may 
vary between versions of the program (different memory
models under MS-DOS is one example, albeit a bad one), but
that's where typedefs come in.


--Joe English

  jeenglis at nunki.usc.edu



More information about the Comp.lang.c mailing list