Variable structure size -- ANSI ?

Harry Protoolis - Sun EHQ harry at matilda.uk.sun.com
Sat Apr 13 05:29:55 AEST 1991


|> Michael N Johnston writes:
|> ]>struct foo {
|> ]>unsigned short recsize;
|> ]>unsigned short num;
|> ]>char info [24];
|> ]>byte flags;
|> ]>char filename[1]
|> ]>};
|> ]>Where, foo.filename is a placeholder for a variable length string.
|> 

A question really, does the standard *guarantee* the physical order of structure
elements ? I looked in K&R 2 and couldn't find anything.

Like, my smart compiler couldn't decide to 'shuffle' them around for reasons
best known to itself could it ?, ... or could it ?

IMHO the technique used above is inferior to using char *filename and mallocing
the space separately simply for reasons of clarity, and if a compiler might 
shuffle structure elements around it could one day lead to some rather curious 
bugs ...

Harry
--- 
(smart Internet mailers) harry.protoolis at uk.sun.com
(smart JANET mailers) harry.protoolis at sun-microsystems.co.uk
(dumb uucp mailers) ...!sun!sunuk!harry.protoolis

'When I give food to the poor they call me a saint.
 When I ask why the poor have no food they call me a communist.'
         - Dom Helder Camara



More information about the Comp.lang.c mailing list