sizeof a struc field
Wm E Davidsen Jr
davidsen at crdos1.crd.ge.COM
Thu Oct 26 04:03:11 AEST 1989
In article <10767 at csli.Stanford.EDU>, poser at csli.Stanford.EDU (Bill Poser) writes:
| I am curious as to when someone would want to take sizeof(((T *) 0)->member).
| If you need to know the size of a structure member, you can always consult
| the structure definition, discover that member is, say, of type "int",
| and write sizeof(int). If you want to allow for future changes in the
| member's type, use the preprocessor and make both the definition and
| the argument to sizeof macros, e.g.
This is just not practical unless you control the header file. There
are times when a field in a structure may not be the same type
(portably). While it's nice to dream of compliant C, you don't run real
software on it. A field might be short on a VAX, int on a PC, and
fubar_t on an ANSI compiler. Therefore, to write code which is maximally
portable you must take the sizeof the field.
No one argued that you can't get the sizeof a field without using a
cast zero with -> to do it, just that it was a useful way to do it, that
most current compilers support it, that no one has proposed a simple
alternative which doesn't increase program size, and I have argued that
that I still think it's legal under the standard.
For all the people who proposed using an extern and not defining it,
that generates a linker error on some systems and is not portable. A
conforming implementation does not have to ignore extern declarations
which are not used.
--
bill davidsen (davidsen at crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon
More information about the Comp.lang.c
mailing list