delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/10/12/08:28:36

Date: Sun, 12 Oct 1997 14:25:21 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: djgpp AT delorie DOT com
Subject: Re: [Q] array declaration
In-Reply-To: <VBh$BHAXb3P0EwDR@foobar.co.uk>
Message-ID: <Pine.SUN.3.91.971012142438.8447S-100000@is>
MIME-Version: 1.0

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019