Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uwm.edu!rpi!crdgw1!crdos1!davidsen From: davidsen@crdos1.crd.ge.COM (Wm E Davidsen Jr) Newsgroups: comp.lang.c Subject: Re: sizeof a struc field Message-ID: <1469@crdos1.crd.ge.COM> Date: 25 Oct 89 18:03:11 GMT References: <7710@microsoft.UUCP> <680011@hpmwjaa.HP.COM> <11404@smoke.BRL.MIL> <10767@csli.Stanford.EDU> Reply-To: davidsen@crdos1.UUCP (bill davidsen) Organization: GE Corp R&D Center Lines: 32 In article <10767@csli.Stanford.EDU>, poser@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@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