用C重新定义枚举/全局枚举值

时间:2015-05-16 15:53:49

标签: c enums pebble-sdk cloudpebble

在我之前提出的一个问题中,我发现无论出于何种原因我都必须使用枚举来定义以下源代码中的值:

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,
    },   
};

在此代码中,意味着在Pebble Watch(云pebble.com)上运行,价值来自在手机上运行的单独的JS应用程序,然后接收该值。但是,如此处所示,我想将该整数转换为枚举器(原因在于:initializer element not constant?)。代码吐出以下错误:

    ../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]

如何将整数转换为枚举器?

1 个答案:

答案 0 :(得分:0)

您的代码显示了对C如何工作的一些基本误解。例如,开关盒中的枚举根本没有意义。枚举在编译时声明值,而在运行时期间使用开关来控制流。

你应该在C上找到一本初学者书,并从一些基本的例子开始。

相关问题