Error: "initializer element is not constant"

Discussion in 'C Programming' started by Todd Nathan, Jul 30, 2003.

  1. Todd Nathan

    Todd Nathan Guest

    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!
    Todd Nathan, Jul 30, 2003
    #1
    1. Advertising

  2. Todd Nathan

    Artie Gold Guest

    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.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

    --
    Artie Gold -- Austin, Texas
    Artie Gold, Jul 30, 2003
    #2
    1. Advertising

  3. Todd Nathan

    Kevin Easton Guest

    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}


    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.
    Kevin Easton, Jul 30, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    509
    Scott David Daniels
    May 30, 2006
  2. Levi Campbell
    Replies:
    3
    Views:
    413
    Flash Gordon
    Feb 11, 2006
  3. Replies:
    3
    Views:
    630
    Wade Ward
    Oct 21, 2007
  4. TheFlyingDutchman

    Tiny C error - initializer element is not constant

    TheFlyingDutchman, Jan 25, 2011, in forum: C Programming
    Replies:
    4
    Views:
    977
    TheFlyingDutchman
    Jan 25, 2011
  5. Bart Vandewoestyne

    error: initializer element is not constant

    Bart Vandewoestyne, Oct 3, 2012, in forum: C Programming
    Replies:
    5
    Views:
    583
    Nobody
    Oct 4, 2012
Loading...

Share This Page