Mail Archives: djgpp/1997/10/12/08:28:36
On Sat, 11 Oct 1997, Paul Shirley wrote:
> <eliz AT is DOT elta DOT co DOT il> writes
> >There are other solutions, but none of them is elegant. This happens
> >to be one of the ugliest problems in C.
>
> The 'elegant' solution is to #define the array size in a header (the
> same one you declare the array seems a good idea;) then use it in both
> the declaration and definition of the array.
This solves the problem with the size, but it still requires you to
put the type of the variable in two different places. What if one day
you decide to make `myarray' int or long or an array of structs?
It gets uglier if you need to initialize the object as well. There's
no good way of doing this in C. The only way I can tolerate uses the
preprocessor, like this:
#ifndef DEFINE_VARS
extern
#endif
char myarray[MYSIZE]
#ifdef DEFINE_VARS
= "abcdefg-xyzzy"
#endif
;
You put this into a header, then in one, and only one, source file
say "#define DEFINE_VARS". This is ugly, but it works.
- Raw text -