我意识到这可能以前已经完成,但是对于这种特殊的皱纹,搜索已经变为空白。
如果我们想要定义一个包含一些字符串的数组,我们可以这样做:
char *strings[] = {"one","two","three"};
如果我们想要定义一个字符串数组数组,我们可以这样做:
char *strings[][] =
{
{"one","two","three"},
{"first","second","third"},
{"ten","twenty","thirty"}
};
但我似乎无法做到这一点:
char *strings[][] =
{
{"one","two"},
{"first","second","third","fourth","fifth"},
{"ten","twenty","thirty"}
};
这样做会引发编译错误。
答案 0 :(得分:2)
来自here,
char *strings[][]
strings
是一个2D数组。
包括,
char *strings[][] =
{
{"one","two","three"},
{"first","second","third"},
{"ten","twenty","thirty"}
};
编译器自动确定strings
中的列数。在这种情况下,每个 strings[i]
是2D数组中的一行。此外,它的类型为char (*string)[3]
的指针(数组名称为指针),即为chare数组为sixe 3。
char *strings[][] =
{
{"one","two"},
{"first","second","third","fourth","fifth"},
{"ten","twenty","thirty"}
};
在这种情况下,编译器无法创建数组(数组必须具有相同类型的元素),因为strings[0]
的类型为char (*strings)[2]
,strings[1]
将是类型char (*strings)[5]
和strings[2]
类型为char (*strings)[3]
因此,编译器说incomplete element type
。
您需要在声明时指定列数(N)(这将使char (*string)[N]
类型的每一行)或动态分配。