Thread: "initializer element is not constant" error

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34

    "initializer element is not constant" error

    I'm _not_ a C-anything programmer - sorry!
    I'm just trying to fix some code so that it will compile.

    It's choking here:

    i3bws.c:49:1: error: initializer element is not constant

    Line 49 is:

    Code:
                                                                              
    Hidden FILE *ws_fp= stderr;       /* where to print workspace name */     
                                      /* may be changed in initbws */
    Is there any more info that I can add, for one of you fine C hackers to give me\
    a clue as to how to fix this issue?

    Much obliged for any help!
    --
    Duke

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    With that much of your code, I cant really see a problem. You need to show us more!

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No the initializer is not a constant... it's another file pointer, that is created when the console opens it's stderr output stream.

    You need to start it off as FILE *ws_fp = NULL; and then assign to it when you open a file.

    I have no idea what the Hidden attribute is about.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34
    Quote Originally Posted by ssharish2005 View Post
    With that much of your code, I cant really see a problem. You need to show us more!
    I'm not sure what you want to see - it's a big project (The ABC programming language)

    Here's more of the relevant "make" output:

    Code:
    make[1]: Entering directory `/home/dnormandin/Downloads/abc/bint3'
    cc -O  -DNDEBUG -I../bhdrs -I../ihdrs -I../unix   -c -o i3bws.o i3bws.c
    i3bws.c:49:1: error: initializer element is not constant
    make[1]: *** [i3bws.o] Error 1
    make[1]: Leaving directory `/home/dnormandin/Downloads/abc/bint3'
    make: *** [bint3] Error 2
    Do you want to see all of "i3bws.c"?
    --
    Duke

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Just a note of caution... the water is VERY deep at that end of the pool... modifying programming languages (esp: compilers) is some of the most difficult programming you can take on. And, in all respect, if you are having problems with simple file pointers...

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34
    I'm just trying to get the compiler's source code to compile on my machine. Otherwise, I wouldn't be doing this.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34
    That fixed that issue! Thanks!

    There more errors - can I post them?

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34
    Hidden FILE *errfile= stderr; /* may be changed in re_errfile() */

    change it to:

    Hidden FILE *errfile= NULL; /* may be changed in re_errfile() */

    ???

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dukester View Post
    Hidden FILE *errfile= stderr; /* may be changed in re_errfile() */

    change it to:

    Hidden FILE *errfile= NULL; /* may be changed in re_errfile() */

    ???
    And then right after that do errfile = stderr; (unless you don't want to get any error messages any more).

    (EDIT: I mean from your ABC thing, not from your compiler, naturally.)

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'm assuming that 'errfile' is global, which is the only reason it would complain about it not being constant. So inside main, assign it stderr before any actual statements happen.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34
    @tabstop

    Very cool! thanks ....

  12. #12
    Registered User
    Join Date
    May 2011
    Location
    Alberta, Canada
    Posts
    34
    Quote Originally Posted by quzah View Post
    I'm assuming that 'errfile' is global, which is the only reason it would complain about it not being constant. So inside main, assign it stderr before any actual statements happen.
    I did what "tabstop" suggested, only to get:

    Code:
    warning: data definition has no type or storage class
    So, if I guess that I need to declare the variable "errfile" with a data-type, right? - where ever I put the code. So what would be the proper declaration, please.
    --
    Duke

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dukester View Post
    I did what "tabstop" suggested, only to get:

    Code:
    warning: data definition has no type or storage class
    So, if I guess that I need to declare the variable "errfile" with a data-type, right? - where ever I put the code. So what would be the proper declaration, please.
    --
    Duke
    If you haven't yet, you need to read Quzah's post -- since this involves actual code, it has to appear inside a function. re_errfile() may or may not be a good place -- the comment certainly suggests that errfile might change there; so depending on how it's written that could be an option.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'FILE *' is its type. I would suggest getting rid of the word 'Hidden' before it, since I doubt that is much more than an alias for 'static'.
    Code:
    #include<stdio.h>
    FILE * errfile = NULL;
    int main( void )
    {
        errfile = stderr;
        return 0;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    And then right after that do errfile = stderr; (unless you don't want to get any error messages any more).

    (EDIT: I mean from your ABC thing, not from your compiler, naturally.)
    Actually he's working on a compiler ... http://en.wikipedia.org/wiki/ABC_(programming_language)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializer element is not constant error - help
    By jay32m in forum C Programming
    Replies: 7
    Last Post: 11-17-2010, 03:24 PM
  2. Replies: 3
    Last Post: 05-01-2010, 02:26 AM
  3. regarding "initializer element is not constant"
    By MK27 in forum C Programming
    Replies: 4
    Last Post: 08-23-2008, 11:30 PM
  4. Replies: 2
    Last Post: 09-12-2006, 04:50 AM
  5. initializer element is not constant
    By lucaspewkas in forum C Programming
    Replies: 4
    Last Post: 05-20-2005, 05:21 AM