No. of elements in initialized array

Joseph S. D. Yao jsdy at hadron.UUCP
Wed Mar 12 23:59:05 AEST 1986


In article <250 at vu-vlsi.UUCP> colin at vu-vlsi.UUCP (Colin Kelley) writes:
>struct termentry { ... } term_tbl[] = { ... };
>  ...  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 is one of the two classic solutions.  The other, if you have code
that steps through your structure, is to have an empty final member of
your structure array.  In your case, { "", 0, 0 }.  Then, code going
through the array can check for *tp->name == NUL, or for tp->xmax ==
0, or whatever you feel is best.

Incidentally, just as a matter of self-preservation in larger projects
(not to mention good style, sanity, etc.), it's been found good to
prefix each member of a structure with a "unique" prefix.  E.g.,
te_name, te_xmax, te_ymax.
-- 

	Joe Yao		hadron!jsdy at seismo.{CSS.GOV,ARPA,UUCP}



More information about the Comp.lang.c mailing list