Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ncar!tank!mimsy!chris From: chris@mimsy.UUCP (Chris Torek) Newsgroups: comp.lang.c Subject: Re: Point me in the right direction Message-ID: <16092@mimsy.UUCP> Date: 23 Feb 89 06:30:02 GMT References: <23631@watmath.waterloo.edu> <1841@mit-caf.MIT.EDU> <1043@auspex.UUCP> <3735@arcturus> Organization: U of Maryland, Dept. of Computer Science, Coll. Pk., MD 20742 Lines: 25 In article <3735@arcturus> evil@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@mimsy.umd.edu Path: uunet!mimsy!chris