Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site vu-vlsi.UUCP Path: utzoo!watmath!clyde!cbosgd!ukma!psuvm.bitnet!psuvax1!vu-vlsi!colin From: colin@vu-vlsi.UUCP (Colin Kelley) Newsgroups: net.lang.c Subject: No. of elements in initialized array Message-ID: <250@vu-vlsi.UUCP> Date: Wed, 26-Feb-86 13:17:02 EST Article-I.D.: vu-vlsi.250 Posted: Wed Feb 26 13:17:02 1986 Date-Received: Fri, 28-Feb-86 22:26:00 EST Organization: Villanova Univ. EE Dept. Lines: 31 Keywords: sizeof, struct We've got some code that looks like: (somewhat simplified here) struct termentry { char name[MAX_ID_LEN]; /* name of graphics driver */ unsigned int xmax,ymax; /* screen dimensions */ } term_tbl[] = { {"hp", HP_XMAX, HP_YMAX}, {"regis", REGIS_XMAX, REGIS_YMAX}, {"tek", TEK_XMAX, TEK_YMAX} }; This code takes advantage of the C compiler's ability to count the number of initializers for term_tbl[], and automatically dimension term_tbl[] to that size. However, code that must look at term_tbl[] needs to know how many elements it has. The solution we're using now is: #define TERMCOUNT (sizeof(term_tbl)/sizeof(struct termentry)) This works fine on both our Pyramid and our Vaxen. BUT...is this the preferred technique? Isn't it possible on some machines that the structs may have to be padded out to some arbitrary word boundary or something in order to fit efficiently into the array (and thus sizeof(term_tbl) may not be an integer multiple of sizeof(struct termentry))? Or will C compilers guarantee that that any necessary padding will be included in sizeof(struct)? I assume this must be a pretty common problem. I hope someone out there can tell me whether our code is ok as is, or if I've overlooked some obvious solution...Thanks! -Colin Kelley ..!{psuvax1,pyrnj}!vu-vlsi!colin