Date: Sun, 12 Oct 1997 14:25:21 +0200 (IST) From: Eli Zaretskii To: djgpp AT delorie DOT com Subject: Re: [Q] array declaration In-Reply-To: Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Precedence: bulk On Sat, 11 Oct 1997, Paul Shirley wrote: > 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.