In 'comp.lang.c',
(Trevor Qi) wrote:
> Hi,dear all:
>
> I have the following question. If I want to initialize an array using
> braces, is there a way that I can define them and give the initial
> value in different lines, like:
>
> int X[5];
> X[5] = {0,0,1,2,9};
>
> But this gives me error message. I know "int X[5] = {0,0,1,2,9};"
> works.
> I really want to do this because this is a public array and will be
> used by many Subroutines.
If it is a read-only data, you can define it
/* in a unique compile unit */
static int const X[] = {0,0,1,2,9};
or
/* shared constant */
int const X[];
with this declaration:
extern int const X[] = {0,0,1,2,9};
Example :
#include <stdio.h>
extern int const X[] = {0,0,1,2,9};
int const X[];
int main (void)
{
int i;
for (i = 0; i < sizeof X / sizeof *X; i++)
{
printf ("X[%d] = %d\n", i, X[i]);
}
return 0;
}
D:\CLC\T\TREVOR>bc
X[0] = 0
X[1] = 0
X[2] = 1
X[3] = 2
X[4] = 9
Or
/* const.c */
#include "const.h"
int const X[];
#ifndef H_ED_CONST_20040514223101
#define H_ED_CONST_20040514223101
/* const.h */
extern int const X[] = {0,0,1,2,9};
#endif /* guard */
/* Guards added by GUARD (c) ED 2000-2003 May 09 2004 Ver. 1.6 */
/* main.c */
#include <stdio.h>
#include "const.h"
int main (void)
{
int i;
for (i = 0; i < sizeof X / sizeof *X; i++)
{
printf ("X[%d] = %d\n", i, X[i]);
}
return 0;
}
--
-ed- get my email here:
http://marreduspam.com/ad672570
The C-language FAQ:
http://www.eskimo.com/~scs/C-faq/top.html
C-reference:
http://www.dinkumware.com/manuals/reader.aspx?lib=c99
FAQ de f.c.l.c :
http://www.isty-info.uvsq.fr/~rumeau/fclc/