Point me in the right direction

Chris Torek chris at mimsy.UUCP
Thu Feb 23 16:30:02 AEST 1989


In article <3735 at arcturus> evil at arcturus.UUCP (Wade Guthrie) writes:
>This is all well and good, but what about arrays of structures?  Am I
>S.O.L. because of inter-array-element padding if I:
>
>	struct already_defined snarf[] = {...};
>
>and then try to get the size by doing:
>
>	#define N_WHATEVER (sizeof(snarf)/sizeof(struct already_defined))

This is guaranteed to work: sizeof() includes any padding.  (But
`(sizeof(snarf) / sizeof(snarf[0]))' is more concise---or substutite
`*snarf' for `snarf[0]' to save a whole two more characters :-) .)

>or can I (portably) [take] sizeof(snarf)/(&snarf[1]-&snarf[0]) ...
>(assuming, of course, that snarf has at least 2 elements. . .)

snarf need not have two elements; the address of the n'th (where the
first is the 0'th) element is computable, and pointer subtraction is
legal.  But this will give you sizeof(snarf)/1.  Also, while I would
have to check the pANS, I believe &var[off] is not a constant, so you
could not use that as (e.g.) a case label.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list