Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!uflorida!uakari.primate.wisc.edu!xanth!kremer From: kremer@cs.odu.edu (Lloyd Kremer) Newsgroups: comp.lang.c Subject: Re: sizeof a struc field Summary: Don't forget structure padding Message-ID: <10201@xanth.cs.odu.edu> Date: 16 Oct 89 20:04:11 GMT References: <7710@microsoft.UUCP> <44200027@uicsrd.csrd.uiuc.edu> <5752@merlin.usc.edu> Distribution: na Organization: Old Dominion University, Norfolk, Va. Lines: 50 In article <5752@merlin.usc.edu> jeenglis@nunki.usc.edu (Joe English) writes: >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)' Neither is adequate if you wish to know the effective size of the member including structure padding. On my 32-bit machine running System V UNIX I get: main() { struct foo { char field; }; printf("sizeof(char) == %d\n", sizeof(char)); printf("sizeof(field) == %d\n", sizeof(((struct foo *)0)->field)); printf("sizeof(struct foo) == %d\n", sizeof(struct foo)); return(0); } Output: sizeof(char) == 1 sizeof(field) == 1 sizeof(struct foo) == 4 -- Lloyd Kremer ...!uunet!xanth!kremer Have terminal...will hack!