gpt4 book ai didi

c - 参数 ‘my 2D-arr’ 已初始化

转载 作者:太空宇宙 更新时间:2023-11-04 07:33:20 24 4
gpt4 key购买 nike

考虑这段代码:

enum 
{
ERR_START,
ERR_CANNOTOPENFILE,
ERR_CANNOTCONNECT,
ERR_CANNOTCONNECTWITH,
ERR_CANNOTGETHOSTNAME,
ERR_CANNOTSEND,

};

char* ERR_MESSAGE[] =
{
[ERR_START] = "Nothing",
[ERR_CANNOTOPENFILE] = "Cannot open '%s' filename.\n",
[ERR_CANNOTCONNECT] = "Cannot connect.\n",
[ERR_CANNOTCONNECTWITH] = "Cannot connect with '%s'.\n",
[ERR_CANNOTGETHOSTNAME] = "Cannot get host name.\n",
[ERR_CANNOTSEND] = "Cannot send.\n",

};

鉴于此错误:

error.h:27:1: error: parameter ‘ERR_MESSAGE’ is initialized
error.h:29:2: error: array index in non-array initializer
error.h:29:2: error: (near initialization for ‘ERR_MESSAGE’)
error.h:29:2: warning: initialization from incompatible pointer type
error.h:30:2: error: array index in non-array initializer
[....]

我正在使用 -std=c99 进行编译。

如何解决这个问题?

最佳答案

我认为您收到此消息是因为文件中存在另一个语法错误(或之前包含的内容)。我将代码粘贴到一个独立的 C 文件中,使用 gcc -std=c99 -c test.c 没有编译错误,我的 GCC 版本是 gcc (GCC) 4.6.3 20120306(红色帽子 4.6.3-2).

一个可能的错误(也许是唯一的错误)是函数声明是否缺少分号。附上代码

enum 
{
ERR_START,
ERR_CANNOTOPENFILE,
ERR_CANNOTCONNECT,
ERR_CANNOTCONNECTWITH,
ERR_CANNOTGETHOSTNAME,
ERR_CANNOTSEND,

};

void foo()
char* ERR_MESSAGE[] =
{
[ERR_START] = "Nothing",
[ERR_CANNOTOPENFILE] = "Cannot open '%s' filename.\n",
[ERR_CANNOTCONNECT] = "Cannot connect.\n",
[ERR_CANNOTCONNECTWITH] = "Cannot connect with '%s'.\n",
[ERR_CANNOTGETHOSTNAME] = "Cannot get host name.\n",
[ERR_CANNOTSEND] = "Cannot send.\n",

};

我得到了类似的错误

est.c: In function ‘foo’:
test.c:13:1: error: parameter ‘ERR_MESSAGE’ is initialized
test.c:15:5: error: array index in non-array initializer
test.c:15:5: error: (near initialization for ‘ERR_MESSAGE’)
test.c:15:5: warning: initialization from incompatible pointer type [enabled by default]
test.c:15:5: warning: (near initialization for ‘ERR_MESSAGE’) [enabled by default]
test.c:16:5: error: array index in non-array initializer
test.c:16:5: error: (near initialization for ‘ERR_MESSAGE’)
test.c:16:5: warning: excess elements in scalar initializer [enabled by default]
test.c:16:5: warning: (near initialization for ‘ERR_MESSAGE’) [enabled by default]
[...]

关于c - 参数 ‘my 2D-arr’ 已初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11268353/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com