initializer element is not constant

This is a discussion on initializer element is not constant within the C Programming forums, part of the General Programming Boards category; I'm trying to define a char* global variable (is this bad practice?) but keep getting an error message "initializer element ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    19

    initializer element is not constant

    I'm trying to define a char* global variable (is this bad practice?) but keep getting an error message "initializer element is not constant"

    the code is as follows:

    Code:
    char* inputfile = malloc( sizeof(char) );
    and is in the header file with other global variables.

    what does the error mean, and why does it only occur when using global variables (i've used the exact same line in a function and it works fine).
    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    char* inputfile = malloc( sizeof(char) );
    Global data must be initialzed with a constant value so that that constant value can be stored in the executable somewhere.
    What exacly do you want to do here? If you just want a char pointer then all you need is
    Code:
    char *inputfile=NULL;

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    Thanks thats all I wanted to do, then I just malloced inside the function. Haven't used global variables much, didn't think about trying to store a non-constant value

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You do know that you are mallocing only one charactor and then storing a pointer to it? seams a waste just for one charactor...

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    19
    yeah, it was all wrong, I need it to be a FILE* anyway...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. initializer element is not constant...
    By John Connor in forum C Programming
    Replies: 12
    Last Post: 02-01-2008, 06:28 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21