Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!nrl-cmf!ames!ucsd!chem.ucsd.edu!tps From: tps@chem.ucsd.edu (Tom Stockfisch) Newsgroups: comp.lang.c Subject: Re: avoiding explicit array sizes (was: Point me in the right direction) Message-ID: <415@chem.ucsd.EDU> Date: 24 Feb 89 00:43:45 GMT References: <23631@watmath.waterloo.edu> <1841@mit-caf.MIT.EDU> <807@atanasoff.cs.iastate.edu> <14647@ccicpg.UUCP> Reply-To: tps@chem.ucsd.edu (Tom Stockfisch) Organization: Chemistry Dept, UC San Diego Lines: 36 In article <14647@ccicpg.UUCP> swine@ccicpg.UUCP (Peter Swain) writes: >In article <807@atanasoff.cs.iastate.edu> hascall@atanasoff.cs.iastate.edu (John Hascall) writes: > [ .. week-long discussion deleted .. ] >>>double fund_consts[] = { >>> 3.14159265, >>>}; >Try using > #define dim(a) (sizeof(a)/sizeof(*(a))) This doesn't work unless the defining declaration of a[] is in the same file. If it is in the same file, it probably should have been declared static. Usually "a" must be declared in a header file which cannot have access to the initialization. The scheme I use is /* header.h */ # define NELT 20 extern int a[NELT]; /* extern.c */ # include "header.h" int a[] = { 42, 666, ... }; /* compiler will complain if sizeof(a)/sizeof(a[0]) is not equal * to NELT, thus ensuring that extern.c and header.h are in sync */ Any file in the project can then get at the dimension of "a" by #include'ing "header.h". "extern.c" must NOT be int a[NELT] = {...}; because if you delete a member of the initialization set, NELT becomes out of sync without the compiler being able to complain. -- || Tom Stockfisch, UCSD Chemistry tps@chem.ucsd.edu