Search code examples
celementinitializer

Initializer element is not constant trying to define defaults for variables- C


Trying to define the defaults for some variables. For example:

static int persist_previousTemp = 1;
static int persist_previousTempDefault = 2;

static int persist_previousIcon = 1;
static int persist_previousIconDefault = 2;

//Define variables for persistent storage of weather, not persistent
static int previousTemp = persist_previousTempDefault;
static int previousIcon = persist_previousIconDefault;

Doing this for pebble development if that makes any difference, but I don't believe it does.

Yes, I've tried searching and previous solutions such as defining within a function just throw me more errors.

Thanks!


Solution

  • You have to initialize globals with constant expressions, variables don't work. If you want to initialize multiple variables using the same value, using #defines is idomatic:

    #define PREV_TEMP_DEFAULT 2
    
    static int previous_temp = PREV_TEMP_DEFAULT;
    static int some_other_global = PREV_TEMP_DEFAULT + 2; /* constant expressions are ok */