Mail Archives: djgpp/1997/10/11/18:46:11
Eli Zaretskii wrote:
> I think that saying "extern char array[];", e.g. in the header
> suggested
> by you, is a better solution, since you don't need to mention the
> dimension twice.
This seems to be the best solution (and what is commonly done). That
is, in the source file, define (and perhaps initialize) the array:
char array[100] = { /* ... */ };
and in the header, simply declare the array (with defining the size):
extern char array[];
There is a precisely analogous situation here to functions and
prototypes. Prototypes (declarations) go in the header; definitions
(the function bodies) go in the source files. (The only clear
difference is that, with prototypes, the `extern' keyword is not
required, but is certainly allowed.)
As for knowing the size of the array outside of the source file in which
the array is defined, you can do that with const int external, e.g., in
the source:
char array[100] = { /* ... */ };
const int arraySize = sizeof array/sizeof array[0];
and in the header:
extern char array[];
extern const int arraySize;
One can obviously use a #define, as well.
--
Erik Max Francis, &tSftDotIotE / mailto:max AT alcyone DOT com
Alcyone Systems / http://newton.alcyone.com/
San Jose, California, United States / icbm://+37.20.07/-121.53.38
\
"After each war there is a little / less democracy to save."
/ Brooks Atkinson
- Raw text -