0

In a previous question I asked, I found out I had to use an enum for whatever reason to define my values in the following source code:

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

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

    //Decide what to do
    switch(key) {
    case key_0:
        enum {key0_buffer = value};      
        break;
    };
}
...
static  WeatherAppDataPoint s_data_points[] = 
{
    {
        ...
        .high = key0_buffer,
    },   
};

In this code, meant to run on the Pebble Watch(cloud pebble.com), value comes from a separate JS app running on a phone and then receives that value. However, as seen here I want to turn that integer into an enumerator(reason is here: initializer element not constant?). The code spits the following errors:

    ../src/app_data.c:120:5: error: a label can only be part of a statement and a declaration is not a statement
../src/app_data.c:120:11: error: enumerator value for 'key0_buffer' is not an integer constant
../src/app_data.c:109:9: error: variable 'value' set but not used [-Werror=unused-but-set-variable]

How can I convert an integer into an enumerator?

Community
  • 1
  • 1
AgentSpyname
  • 105
  • 1
  • 9
  • That doesn't make much sense. Looks like you're trying to initialize a global static variable with something that apparently will only be set once a function is called (i.e. at runtime). Enums are constants, you can't change their value. – Mat May 16 '15 at 16:02
  • @Mat Thanks a lot. That helped, I think I have to modify the structure of this code. – AgentSpyname May 16 '15 at 16:09

1 Answers1

0

Your code shows some fundamental misunderstandings of how C works. For example the enum in the switch case does not make sense at all. The enum declares values at compile time, while a switch is used during runtime for control flow.

You should find a beginners book on C and start with some basic examples.

Nikolai Ruhe
  • 81,520
  • 17
  • 180
  • 200
  • 1
    Yes, its true, im more of a JS, Python and Rails kinda person, working with the more higher level languages. The book on C I have never talked about Enums... But thanks for that advice anyway, I'm probably going to buy a better book. – AgentSpyname May 16 '15 at 16:11