Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Error: "initializer element is not constant"

Reply
Thread Tools

Error: "initializer element is not constant"

 
 
Todd Nathan
Guest
Posts: n/a
 
      07-30-2003
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!
 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      07-30-2003
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

 
Reply With Quote
 
 
 
 
Kevin Easton
Guest
Posts: n/a
 
      07-30-2003
Todd Nathan <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Element ScriptManager is not a known element John Kotuby ASP .Net 2 09-09-2010 01:14 PM
Element is not a known element. This can occur if there is a compilation error in the Web site MK ASP .Net 5 06-27-2008 07:20 PM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Solution: "Element '%' is not a known element. This can occur if there is a compilation error in the Web site" hirebrian@hotmail.com ASP .Net 0 08-14-2007 12:51 AM
Installing Ajax RC1 - ---- Element 'ScriptManager' is not a known element Elmo Watson ASP .Net 1 01-19-2007 12:01 AM



Advertisments