469,270 Members | 1,818 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Error: "initializer element is not constant"

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[i].sfn,
2 * sizeof(stackitem))) != NULL) {
stackitem *si = atl_body(dw);
*si++ = FileSent;
*si = (stackitem) stdfiles[i].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!
Nov 13 '05 #1
2 18894
Todd Nathan wrote:
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[i].sfn,
2 * sizeof(stackitem))) != NULL) {
stackitem *si = atl_body(dw);
*si++ = FileSent;
*si = (stackitem) stdfiles[i].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

--
Artie Gold -- Austin, Texas

Nov 13 '05 #2
Todd Nathan <bi***********@yahoo.com> wrote:
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.

Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by emin.martinian | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.