Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!caen!spool.mu.edu!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: Help needed with: sizeof (struct a) Keywords: sizeof,struct,help,cc Message-ID: <6398@goanna.cs.rmit.oz.au> Date: 20 Jun 91 07:11:47 GMT Article-I.D.: goanna.6398 References: <10569@aspect.UUCP> Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 29 In article <10569@aspect.UUCP>, dave@aspect.UUCP (Dave Corcoran) writes: > My Sun cc prints 4 for both structs, prints 3 for the array > struct a {char x;char y;char z}; > struct b {char x[3]}; > char c[3]; Syntax error: that should be struct a {char x; char y; char z;}; ^ and the same for struct b. Omitting the last ; before the } of a struct was a Berkeley extension. Many compilers do not support it, and it is not standard. > Is there any way to cause cc to force sizeof return the actual size of the > structs without rounding up to the next highest sizeof (short)? Four bytes *IS* the actual size of the structs in that implementation of C. Remember, "sizeof (T)" means "if you had an array of N elements each of type (T), the number of bytes in the array would be N times ?". If you have an array of (struct a)s, they will be 4 bytes apart, not 3. There isn't anything you can portably do that would profit from trying to outsmart the compiler in this way, and very little that is _safe_ even for a program that is never going to see another compiler or machine. (You could try taking offsetof the last field + sizeof the last field. But of course the last field itself may contain padding.) -- I agree with Jim Giles about many of the deficiencies of present UNIX.