Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

initializer element not constant?

Tags:

c

pebble-sdk

I am relatively knew to C and only learning pieces of it to publish a Pebble C/PebbleKitJS app to track buses. So far I have the data being processed on a Node server, and I am getting ready to have the data processed by a JS File. MY one problem however lies within the C Code.

This code process data stored in a Key Dictionary sent from JS and assigns it to a variable for use below. By using #define var 9, I can successfully have the .high value set to 9. But through an int var, it fails and throws the error:initializer element not constant?? .

What does this error mean, and what exactly is the difference between static and constant if i don't define it. apparently static vars don't return anything? Some help would be much appreciated.

UPDATE: The problem still isn't fixed. The following new error message occurs in addition to the initializer one. error: (near initialization for 's_data_points[0].high')

   int key0_buffer; 


  void process_tuple(Tuple *t)
{
    //Get key
    int key = t->key;

    //Get integer value, if present
     int value = t->value->int32;

    //Get string value, if present
    char string_value[32];
    strcpy(string_value, t->value->cstring);

    //Decide what to do
    switch(key) {
        case key_0:
            //Location received
            key0_buffer = value;
            break;
  }



  }

static WeatherAppDataPoint s_data_points[] = {

  {
 .city = "San Diego",
     .description = "surfboard :)",
        .icon = WEATHER_APP_ICON_GENERIC_WEATHER,
        .current = 110,
        .high = key0_buffer,
        .low = 9,
  },   
};
like image 274
AgentSpyname Avatar asked Feb 11 '26 05:02

AgentSpyname


1 Answers

Try this instead:

enum { key0_buffer = 9 };
  • C doesn't provide for runtime computations while initializing global variables. (The concept does exist as a C++ feature called "dynamic initialization.")

    The execution model is that it can store all the bytes of global variables in ROM, and then copy any modifiable variables into RAM together with a single memcpy. Assigning one global to another would be more complicated.

  • #define allows you to substitute the text 9, which is a constant expression.

    Many frown upon using text substitutions to avoid variables as primitive, unnecessarily low-level, and potentially inefficient. In this case though, the results should be the same.

  • In C, enum constants have type int, so they are a suitable substitute. You're out of luck for other types, though.

like image 102
Potatoswatter Avatar answered Feb 17 '26 22:02

Potatoswatter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!