1

我意识到这可能以前已经完成了,但是对于这种特定的皱纹,搜索却变成了空白。

如果我们想定义一个包含一些字符串的数组,我们可以这样做:

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"}
};

这样做会引发编译器错误。

(多维数组中的字符串初始化示例)

4

1 回答 1

2

这里开始

char *strings[][] 

strings是一个二维数组。

的情况下,

char *strings[][] =
{
  {"one","two","three"},
  {"first","second","third"},
  {"ten","twenty","thirty"}
};

编译器会自动确定strings. 在这种情况下,每个 strings[i]都是二维数组中的一行。此外,它是一个类型的指针(数组名称是指针),char (*string)[3]即指向 Sixe 3 的 char 数组。

char *strings[][] =
{
  {"one","two"},
  {"first","second","third","fourth","fifth"},
  {"ten","twenty","thirty"}
};

在这种情况下,编译器无法创建数组(数组必须具有相同类型的元素),因为strings[0]将是 type char (*strings)[2]strings[1]将是 typechar (*strings)[5]并且strings[2]将是 typechar (*strings)[3]

因此,编译器说incomplete element type.

您需要在声明时指定列数 (N)(这将使每一行的 type char (*string)[N])或动态分配。

于 2013-06-11T14:37:22.590 回答