Is this bad programming?
Karl Heuer
karl at haddock.ima.isc.com
Thu Aug 9 09:20:49 AEST 1990
In article <1990Aug8.100614.1223 at resam.dk> andrew at 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 at kelp.ima.isc.com or ima!kelp!karl), The Walking Lint
More information about the Comp.lang.c
mailing list