Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!umich!samsung!usc!snorkelwacker!spdcc!ima!haddock!karl From: karl@haddock.ima.isc.com (Karl Heuer) Newsgroups: comp.lang.c Subject: Re: Is this bad programming? Keywords: #define array definition Message-ID: <17300@haddock.ima.isc.com> Date: 8 Aug 90 23:20:49 GMT References: <1990Aug8.100614.1223@resam.dk> Reply-To: karl@kelp.ima.isc.com (Karl Heuer) Organization: Interactive Systems, Cambridge, MA 02138-5302 Lines: 32 In article <1990Aug8.100614.1223@resam.dk> andrew@resam.dk (Leif Andrew Rump) writes: >[To avoid having #define indices get out of sync with constant arrays,] > char errortext[][40] = { > "234567890123456789012345678901234567890", > #define FILE_FOOTER_ERROR 1 > "File footer error", > #define DRAW_FOOTER_ERROR 2 > "Draw footer error", > } Yeah, it works; I don't recommend it. It doesn't work as well if you have *two* arrays that use the same enumerated indices, for example; or if you want to put the indices and the strings in separate files. I would rather keep the definitions separate, and add a comment like `/* must be kept in sync! */'. I recently proposed an extension to allow labeled initializers: #define FILE_FOOTER_ERROR 1 #define DRAW_FOOTER_ERROR 2 char errortext[3][40] = { 0: "234567890123456789012345678901234567890", DRAW_FOOTER_ERROR: "Draw footer error", FILE_FOOTER_ERROR: "File footer error", }; This might make it into gcc (and maybe from there into C-2001?). >I am a pascal-programmer I might have guessed. A C programmer would likely have used `char *[]' (instead of padding all the strings to the same length like you have to do in non-extended Pascal) and made the indices start at zero. Karl W. Z. Heuer (karl@kelp.ima.isc.com or ima!kelp!karl), The Walking Lint