参数'my 2D-arr'已初始化

时间:2012-06-29 20:11:26

标签: c arrays cygwin c99

考虑以下代码:

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进行编译。

如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

我认为您收到此消息是因为文件中的另一个语法错误(或之前包含的内容)。我将代码粘贴到一个独立的C文件中,并且gcc -std=c99 -c test.c没有编译错误,我的GCC版本是 gcc(GCC)4.6.3 20120306(Red Hat 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]
[...]