初始元素不是常数? [英] initializer element not constant?
问题描述
我是比较知道到C,只学习它的一部分发布一个卵石C / PebbleKitJS应用程序来跟踪总线。到目前为止,我有一个节点服务器上被处理的数据,我准备有一个JS文件处理的数据。但是我唯一的问题在于在C $ C $℃以内。
存储在密钥字典此code处理来自JS发送并将其分配给下面使用的变量。通过使用#define VAR 9,我可以成功有设定为9。但通过一个int VAR的。高价值,它失败并引发错误:初始元素不是常数? 。
这是什么意思的错误,而究竟是静态的,如果我不把它定义常量之间的差异。显然静态瓦尔不返回任何?一些帮助将是非常美联社preciated。
更新:问题仍然是不固定的。除了初始化之一发生了以下新的错误消息。 错误:(近初始化s_data_points [0]。高')
INT key0_buffer;
无效process_tuple(数组* T)
{
//获取关键
INT键= T->的关键; //获取整数值,如果present
int值= T->&值 - GT; INT32; //获取字符串值,如果present
焦炭STRING_VALUE [32];
的strcpy(STRING_VALUE,T->&值 - GT; CString的); //决定做什么
开关(键){
案例KEY_0:
//获得位置
key0_buffer =价值;
打破;
} }静态WeatherAppDataPoint s_data_points [] = { {
。市=圣迭戈
.DESCRIPTION =冲浪板:)
.icon = WEATHER_APP_ICON_GENERIC_WEATHER,
.current = 110,
。高= key0_buffer,
。低= 9,
},
};
试试这个:
枚举{key0_buffer = 9};
-
在初始化的全局变量C不提供运行时间计算。 (这个概念确实存在作为称为C ++功能动态初始化。)
执行模型是,它可以存储在ROM中的全局变量的所有字节,然后任何可修改的变量与单个
的memcpy
一起复制到RAM中。分配一个全局另一个会更复杂。 -
的#define
允许你替换文本9
,这是一个不变的前pression。在使用文本替换,以避免变量作为原始的,不必要的低水平,并有可能效率低下,许多皱眉。在这种情况下,虽然,结果应该是相同的。
-
在C,
枚举
常量的类型INT
,所以他们是一个合适的替代品。你的运气其他类型的,虽然。
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,
},
};
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 text9
, 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 typeint
, so they are a suitable substitute. You're out of luck for other types, though.
这篇关于初始元素不是常数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!