相关疑难解决方法(0)

警告:初始化元素不是常量

所有,

在编译时,我有以下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)

c

2
推荐指数
1
解决办法
1614
查看次数

在C中创建一个char []数组

我正在使用一个需要提供大型(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>&nbsp;\
<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]')".两者都出现在最后一行.

c string variable-assignment

1
推荐指数
1
解决办法
156
查看次数

使用数组中的数据初始化结构

我想将我的关键字保存在结构中:

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 arrays struct

0
推荐指数
1
解决办法
86
查看次数

如何在c中声明数组

我在学习c时仍然知道,我面临以下问题.

我试图初始化声明一个数组但它给我编译错误.

const   int a =2;
int x[a]={2};
Run Code Online (Sandbox Code Playgroud)

c arrays

-2
推荐指数
1
解决办法
60
查看次数

标签 统计

c ×4

arrays ×2

string ×1

struct ×1

variable-assignment ×1