Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!think!harvard!seismo!rlgvax!hadron!jsdy From: jsdy@hadron.UUCP (Joseph S. D. Yao) Newsgroups: net.lang.c Subject: Re: No. of elements in initialized array Message-ID: <295@hadron.UUCP> Date: Wed, 12-Mar-86 08:59:05 EST Article-I.D.: hadron.295 Posted: Wed Mar 12 08:59:05 1986 Date-Received: Sat, 15-Mar-86 19:52:43 EST References: <250@vu-vlsi.UUCP> Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao) Organization: Hadron, Inc., Fairfax, VA Lines: 19 Keywords: sizeof, struct Summary: Two ways: yours, and the other. In article <250@vu-vlsi.UUCP> colin@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@seismo.{CSS.GOV,ARPA,UUCP}