所有,
在编译时,我有以下C代码,
static struct
{
const char* val;
const char* parse_key;
int len; //parse key length
void (*parse_routine) (const char* after, rtsp_sdp_t* response);
} sdp_header[] =
{
#define SDP_FILL_STRUCT(a,b) {#a, b, strlen(b), sdp_##a}
SDP_FILL_STRUCT(attr_control, "a=control:"),
SDP_FILL_STRUCT(attr_framerate,"a=framerate:"),
SDP_FILL_STRUCT(attr_range, "a=range:"),
{'\0', '\0', 0, (void*)0},
};
Run Code Online (Sandbox Code Playgroud)
为什么它会发出以下错误:
:330: warning: initializer element is not constant
:330: warning: (near initialization for 'sdp_header[0]')
:331: warning: initializer element is not constant
:331: warning: (near initialization for 'sdp_header[1]')
:332: warning: initializer element is not constant
:332: warning: (near …Run Code Online (Sandbox Code Playgroud) 我正在使用一个需要提供大型(20kb)静态html页面的wifi微控制器.因为微控制器上的各个缓冲区只能容纳1.4kb,所以有必要将html拆分成块并一次发送一个.
我现在拥有的是大约100个字符串assignemnts,如下所示:
char HTML_ID_96[] = "\
<p><a href=\"#t\">Return to top</a></p>\
<a id=\"id9\"/>\
<span class=\"s\">Firmware Version/Information</span>\
<span class=\"c i\" id=\"id9-h\" onclick=\"h(\'id9\');\">hide</span> \
<span class=\'c\' id=\"id9-s\" onclick=\"s(\'id9\');\">show</span>\
<table class=\"t i\" id=\"id9-table\"><tbody>\
";
Run Code Online (Sandbox Code Playgroud)
我想通过将它们粘贴在一个数组中来对所有字符串强加一个可迭代序列的方法,但我不知道如何打包它们.
我试过了:
char** all = [HTML_ID_1, ..., HTML_ID_99];
char* all[] = [HTML_ID_1, ..., HTML_ID_99];
char all[][] = [HTML_ID_1, ..., HTML_ID_99];
Run Code Online (Sandbox Code Playgroud)
但他们都没有编译.任何关于C如何处理数组的引用都是一个奖励.
延期:
char const* HTML_ID_100 = "\
</form>\
</body>\
</html>\
";
char const* all[] = {HTML_ID_100};
Run Code Online (Sandbox Code Playgroud)
不编译.我正在使用gcc 3.4.4.报告了两个错误:"初始化元素不是常量"和"(接近初始化'all [0]')".两者都出现在最后一行.
我想将我的关键字保存在结构中:
typedef const char* label;
const struct keywords_t
{
label init;
label moveUnit;
} keywords;
Run Code Online (Sandbox Code Playgroud)
但我希望能够检查关键字是否对for循环有效,所以我尝试初始化这样的结构:
const label allowed_keywords[] =
{
"INIT",
"MOVE"
};
const struct keywords_t keywords =
{
allowed_keywords[0],
allowed_keywords[1]
};
Run Code Online (Sandbox Code Playgroud)
这给了我一个
error: initializer element is not constant
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?但是整个数组是不变的.
我在学习c时仍然知道,我面临以下问题.
我试图初始化并声明一个数组但它给我编译错误.
const int a =2;
int x[a]={2};
Run Code Online (Sandbox Code Playgroud)