Error: "initializer element is not constant"

T

Todd Nathan

Hi. have this code and compiler problem. GCC 2.95.3, BeOS, error
"initializer element is not constant"

[ ... 3000 + lines deleted for brevity ... ]

#ifdef FILEIO
{ static struct {
char *sfn;
FILE *sfd;
} stdfiles[] = {
{"STDIN", stdin},
{"STDOUT", stdout},
{"STDERR", stderr}
};
int i;
dictword *dw;

for (i = 0; i < ELEMENTS(stdfiles); i++) {
if ((dw = atl_vardef(stdfiles.sfn,
2 * sizeof(stackitem))) != NULL) {
stackitem *si = atl_body(dw);
*si++ = FileSent;
*si = (stackitem) stdfiles.sfd;
}
}
}

[ ... to end of source file deleted for brevity ... ]

------------

and this compiler problem...

[beos@dualP2 ~/forth/atlast-1.0] $ make
cc -O -DMEMSTAT -DALIGNMENT -DEXPORT -c atlast.c -o atlast.o
atlast.c: In function `atl_init':
atlast.c:3247: initializer element is not constant
atlast.c:3247: (near initialization for `stdfiles[0].sfd')
atlast.c:3248: initializer element is not constant
atlast.c:3248: (near initialization for `stdfiles[1].sfd')
atlast.c:3249: initializer element is not constant
atlast.c:3249: (near initialization for `stdfiles[2].sfd')
make: *** [atlast.o] Error 1


I see the property list being built, and shouldnt the FILE* defined as
an element of each pair be enough to allow stdin, stdout and stderr
to be assigned? This is not my code, a package I'm porting. How
to get this to compile would be greatly appreciated, email me directly
please, I'm too ashamed to speak again here publically about this :)

Thank you!
 
A

Artie Gold

Todd said:
Hi. have this code and compiler problem. GCC 2.95.3, BeOS, error
"initializer element is not constant"

[ ... 3000 + lines deleted for brevity ... ]

#ifdef FILEIO
{ static struct {
char *sfn;
FILE *sfd;
} stdfiles[] = {
{"STDIN", stdin},
{"STDOUT", stdout},
{"STDERR", stderr}
};
int i;
dictword *dw;

for (i = 0; i < ELEMENTS(stdfiles); i++) {
if ((dw = atl_vardef(stdfiles.sfn,
2 * sizeof(stackitem))) != NULL) {
stackitem *si = atl_body(dw);
*si++ = FileSent;
*si = (stackitem) stdfiles.sfd;
}
}
}

[ ... to end of source file deleted for brevity ... ]

------------

and this compiler problem...

[beos@dualP2 ~/forth/atlast-1.0] $ make
cc -O -DMEMSTAT -DALIGNMENT -DEXPORT -c atlast.c -o atlast.o
atlast.c: In function `atl_init':
atlast.c:3247: initializer element is not constant
atlast.c:3247: (near initialization for `stdfiles[0].sfd')
atlast.c:3248: initializer element is not constant
atlast.c:3248: (near initialization for `stdfiles[1].sfd')
atlast.c:3249: initializer element is not constant
atlast.c:3249: (near initialization for `stdfiles[2].sfd')
make: *** [atlast.o] Error 1


I see the property list being built, and shouldnt the FILE* defined as
an element of each pair be enough to allow stdin, stdout and stderr
to be assigned? This is not my code, a package I'm porting. How
to get this to compile would be greatly appreciated, email me directly
please, I'm too ashamed to speak again here publically about this :)

By the standard, none of stdin, stdout or stderr need necessarily be
constants -- though they are in many implementations.

HTH,
--ag
 
K

Kevin Easton

Todd Nathan said:
Hi. have this code and compiler problem. GCC 2.95.3, BeOS, error
"initializer element is not constant"

[ ... 3000 + lines deleted for brevity ... ]

#ifdef FILEIO
{ static struct {
char *sfn;
FILE *sfd;
} stdfiles[] = {
{"STDIN", stdin},
{"STDOUT", stdout},
{"STDERR", stderr}

stdin, stdout and stderr aren't guaranteed to be compile time constants
- and you need compile time constants for aggregate initialisers.

So - the code should be written this way (this will work anywhere, so
should be fixed in the original codebase too):

{ static struct {
char *sfn;
FILE *sfd;
} stdfiles[] = {
{"STDIN", NULL },
{"STDOUT", NULL },
{"STDERR", NULL }
};

int i;
dictword *dw;

stdfiles[0].sfd = stdin;
stdfiles[1].sfd = stdout;
stdfiles[2].sfd = stderr;

/* ... */

[...]
How to get this to compile would be greatly appreciated, email me
directly please, I'm too ashamed to speak again here publically about
this :)

Sorry, post here, read here - consider the next guy who comes along with
the same problem.

- Kevin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top