Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!gem.mps.ohio-state.edu!usc!merlin.usc.edu!nunki.usc.edu!jeenglis From: jeenglis@nunki.usc.edu (Joe English) Newsgroups: comp.lang.c Subject: Re: sizeof a struc field Message-ID: <5752@merlin.usc.edu> Date: 14 Oct 89 00:03:21 GMT References: <7710@microsoft.UUCP> <44200027@uicsrd.csrd.uiuc.edu> <1075@crdos1.crd.ge.COM> Sender: news@merlin.usc.edu Reply-To: jeenglis@nunki.usc.edu (Joe English) Distribution: na Lines: 34 davidsen@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@nunki.usc.edu