Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!munnari.oz.au!goanna!ok From: ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) Newsgroups: comp.lang.c Subject: Re: How to force cpp to abort? Message-ID: <3579@goanna.cs.rmit.oz.au> Date: 18 Aug 90 09:29:16 GMT References: <9679@ganymede.inmos.co.uk> Distribution: comp Organization: Comp Sci, RMIT, Melbourne, Australia Lines: 68 In article <9679@ganymede.inmos.co.uk>, nathan@elberton.inmos.co.uk (Nathan Sidwell) writes: > but it struck me that [using #error] also helps in another > thread about array initializing. When I define an initialized array in a > header file, and initialize it in the code file, the two must be kept in step. > /* module.h */ > > #define DETECTORS 7 > extern char *detectors[DETECTORS]; > /* module.c */ > > #if DETECTORS != 7 > #error Module assumes DETECTORS == 7 > #endif > extern char *detectors[DETECTORS] = > { /* 7 strings deleted */ > }; For an array with 2 or more dimensions, you haven't the choice, but for a one-dimensional array a better method is just to write extern char *detectors[]; in the header file. With the code as presented, if DETECTORS is too small, the C compiler is going to report an error anyway. I feel rather uncomfortable at having the master definition for the contents of the table in one place and the master definition for the size of the table in another. The little AWK program I posted a while back for turning tables into C has a smaller brother, which generates header files from the same tables. Recall the example: #File: mktable.demo EntryCt; int whence; int whither; char *name Butcher; 1; 2; "Butcher" Baker; 1; 3; "Baker" Bellman; 2; 3; "Bellman" Boojum; 0; 0; NULL #End: mktable.demo Running the command awk -F";" -f mkheader.awk mktable.demo produces /* From mktable.demo */ #define Butcher 0 #define Baker 1 #define Bellman 2 #define Boojum 3 #define EntryCt 4 extern int whence[4]; extern int whither[4]; extern char *name[4]; /* End mktable.demo */ automagically. (If any of the table names has "static" in it, that table name is not written out by mkheader.awk.) Having "make" generate the C initialisations and the header file automatically from a common source is one way of making sure that they *can't* get out of step. -- The taxonomy of Pleistocene equids is in a state of confusion.