How to force cpp to abort?

Richard A. O'Keefe ok at goanna.cs.rmit.oz.au
Sat Aug 18 19:29:16 AEST 1990


In article <9679 at ganymede.inmos.co.uk>, nathan at 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.



More information about the Comp.lang.c mailing list