#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Location
    Wisconsin, USA
    Posts
    41
    Rep Power
    7

    Error: initializer element is not constant


    first the offending code
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    const unsigned int dim = 500;
    unsigned char* pixmap = malloc(dim * dim * 3);
    int main()
    {
    
            return 0;
    }
    test.c:5: error: initializer element is not constant

    I'm on a Linux box, using GCC. I have these as globals as I need methods from other files to have access to them (something I've never really done before, so at first I thought that was the problem... but there aren't any other files here, so that can't really be it)... so, what's wrong? I mean, I thought the whole point of malloc was that it didn't have to be a constant... initializer element? Actually, what is that? Are we talking the pointer reference? Is it against the rules to have a global pointer like this? Let me go check real quick with our mutual friend google... mm, I didn't see anything that I understood... (okay, I understood what I saw, and realized that most of it wasn't pertinent, and the rest... was totally unrelated) Maybe it's just my search terms... anyways, advice? Alternatives if this is not feasible? And, since I know you can only help me if I help you...

    I need the dim variable to be accessible to other files, and I need the pixmap variable to be accessible to other files. I even tried using literals in place of dim when calling malloc(), and that didn't change a thing...

    Thanks again,

    tredontho
  2. #2
  3. Super User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Posts
    648
    Rep Power
    76
    You can't call functions from a global scope. To fix this problem, call malloc() from within main() or some other function scope:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    const unsigned int dim = 500;
    unsigned char* pixmap;
    int main()
    {
            pixmap = malloc(dim * dim * 3);
            return 0;
    }
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2007
    Location
    Wisconsin, USA
    Posts
    41
    Rep Power
    7
    Ah, gotcha. Thanks. I did that, and it worked, but it's nice to know WHY it didn't work in the first place... I hate fixing something and not knowing why it was fixed, or what was wrong to start with.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4247
    The reason it didn't work for you the first time is because the variables declared outside main are global variables. Global variables are initialized at compile time. Hence you cannot assign a global variable to the result of a function, since the function needs to be called at run-time and the return value of the function cannot be predicted at compile time (Also note that the function call might fail and return NULL)

    On the other hand, when you place the variable inside the main() function, the following code is equivalent:
    Code:
    {
        char *pixmap;
        pixmap = malloc(somesize);
    }
    and
    Code:
    {
        char *pixmap = malloc(somesize);
    }
    In either case, it is calling malloc() at runtime and then assigning the result of the function to pixmap.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo