Initialization element is not persistent?

I am relatively aware that C and only parts of it to publish a Pebble C / PebbleKitJS bus tracking application. As long as I have the data being processed on the Node server and I'm ready to process the data using a JS file. My one problem, however, lies in the C code.

This code process data is stored in a key dictionary sent from JS and assigns it to the variable used below. Using #define var 9, I can successfully set the .high value to 9. But via int var it fails and throws an error: initialization element is not a 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 return nothing? Some help would be much appreciated.

UPDATE: The issue is still not fixed. In addition to the initializer, the following new error message appears.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;


static WeatherAppDataPoint s_data_points[] = {

 .city = "San Diego",
     .description = "surfboard :)",
        .current = 110,
        .high = key0_buffer,
        .low = 9,



source to share

2 answers

Try this instead:

enum { key0_buffer = 9 };


  • C does not provide run-time computation when initializing global variables. (The concept exists as a C ++ feature called "dynamic initialization".)

    The execution model is that it can store all bytes of global variables in ROM and then copy any mutable variables to RAM along with one memcpy

    . Assigning one global to another will be more complicated.

  • #define

    allows you to substitute text 9

    that is a constant expression.

    Many are unhappy with the use of text substitutions to avoid variables as primitive, unnecessarily low-level, and potentially ineffective. In this case, however, the results should be the same.

  • In C, enum

    constants are of type int

    , so they are suitable substitutions. However, you are out of luck with other types.



There is a key difference between the code in your function and the subscript of the code defining a static variable. The function code is executable - these lines will run when the function is called.

Your static declaration WeatherAppDataPoint simply tells the compiler to create a static variable. The initialization values ​​that you place in this declaration tell the compiler what value to initialize this data. This is why they need to be constant - they are values ​​that are loaded before anything is done.

The #define statement simply tells the preprocessor to replace all instances of "var" with the string "9". It is literally the same as cutting and pasting in a text editor. This is done before the compiler ever sees the code. This is why you have no problem with it; the compiler sees a literal constant, just as if you manually typed "9" directly into the source code.

In 'C' variables don't return things, they store them. Only functions return things. If you want an integer to be declared somewhere, then assign it to the value of your static variable, this is the actual executable line of code that needs to be executed inside a function somewhere (i.e. inside your process_tuple function). The lines of code below the static declaration are not executed at runtime, they simply set the initial state of the program and tell the compiler how large the variable is.



All Articles