3
    char monsternivel1[][3][4] = {
    {"Rat","Bat","Spider"},
    {"Goblin","Orc","Drawf"},
    {"Dragon","Lich","Banshee"},
    {"Demon","Hydra","Giant Spider"}
    };

它说 :

> E:\Dungeon Crawler.c||In function 'rndMonster':| E:\Dungeon
> Crawler.c|10|warning: initializer-string for array of chars is too
> long [enabled by default]| E:\Dungeon Crawler.c|10|warning: (near
> initialization for 'monsternivel1[0][2]') [enabled by default]|
> E:\Dungeon Crawler.c|11|warning: initializer-string for array of chars
> is too long [enabled by default]| E:\Dungeon Crawler.c|11|warning:
> (near initialization for 'monsternivel1[1][0]') [enabled by default]|
> E:\Dungeon Crawler.c|11|warning: initializer-string for array of chars
> is too long [enabled by default]| E:\Dungeon Crawler.c|11|warning:
> (near initialization for 'monsternivel1[1][2]') [enabled by default]|
> E:\Dungeon Crawler.c|12|warning: initializer-string for array of chars
> is too long [enabled by default]| E:\Dungeon Crawler.c|12|warning:
> (near initialization for 'monsternivel1[2][0]') [enabled by default]|
> E:\Dungeon Crawler.c|12|warning: initializer-string for array of chars
> is too long [enabled by default]| E:\Dungeon Crawler.c|12|warning:
> (near initialization for 'monsternivel1[2][2]') [enabled by default]|
> E:\Dungeon Crawler.c|13|warning: initializer-string for array of chars
> is too long [enabled by default]| E:\Dungeon Crawler.c|13|warning:
> (near initialization for 'monsternivel1[3][0]') [enabled by default]|
> E:\Dungeon Crawler.c|13|warning: initializer-string for array of chars
> is too long [enabled by default]| E:\Dungeon Crawler.c|13|warning:
> (near initialization for 'monsternivel1[3][1]') [enabled by default]|
> E:\Dungeon Crawler.c|13|warning: initializer-string for array of chars
> is too long [enabled by default]| E:\Dungeon Crawler.c|13|warning:
> (near initialization for 'monsternivel1[3][2]') [enabled by default]|

我不明白 char 的 3d 数组,有什么想法吗?第一个 [] 应该是变量,第二个 [] 应该是多少个字符串,第三个应该是类别?

4

3 回答 3

5

你想做什么可能:

const char *monsternivel1[4][3] = {
    {"Rat","Bat","Spider"},
    {"Goblin","Orc","Drawf"},
    {"Dragon","Lich","Banshee"},
    {"Demon","Hydra","Giant Spider"}
  };

它仍然是一个带有 char* 的二维数组。请注意,二维数组的顺序是[row][column]而不是[column][row]

于 2013-11-06T22:20:52.950 回答
3

如果你想要一个 3D 数组,那么每个字符串的最后一个维度必须足够大:

char monsternivel1[][3][13] = {
    { "Rat", "Bat", "Spider" },
    { "Goblin", "Orc", "Dwarf" },
    { "Dragon", "Lich", "Banshee" },
    { "Demon", "Hydra", "Giant Spider" },
};

注意:'drawf' -> 'dwarf'。

于 2013-11-06T22:25:20.857 回答
1

正如错误消息告诉您的那样,您尝试初始化的数组不足以容纳这些字符串的所有字符。

例如,“Giant Spider”需要 13 个字符,当包括终止空字符时,'\0'.

尝试以下操作:

char monsternivel1[4][3][13] = {
    {"Rat","Bat","Spider"},
    {"Goblin","Orc","Drawf"},
    {"Dragon","Lich","Banshee"},
    {"Demon","Hydra","Giant Spider"}
 };

考虑这一点的方法是根据指针。不确定您是否熟悉它们,但 C 中的字符数组是作为指向数组中第一个字符的指针实现的。

因此,数组的第一个索引指定指向字符指针数组的指针数。

第二个指定每个指针数组中指向字符数组的指针的数量。

最后,第三个索引指定包含数据的最终数组中的最大字符数(长度)。

是否需要 2-D 指针数组或 3-D 字符数组取决于您计划在程序中的其他位置处理数据。

于 2013-11-06T22:24:30.303 回答