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]|
我想我不理解任何想法的3D数组? 第一个[]是可变的secod []第三类有多少个字符串?
答案 0 :(得分:3)
你想做什么:
const char *monsternivel1[4][3] = {
{"Rat","Bat","Spider"},
{"Goblin","Orc","Drawf"},
{"Dragon","Lich","Banshee"},
{"Demon","Hydra","Giant Spider"}
};
它仍然是一个带有char *的二维数组。请注意,二维数组的顺序为[row][column]
而不是[column][row]
,
答案 1 :(得分:2)
如果你想要一个3D数组,那么最后一个维度对于每个字符串必须足够大:
char monsternivel1[][3][13] = {
{ "Rat", "Bat", "Spider" },
{ "Goblin", "Orc", "Dwarf" },
{ "Dragon", "Lich", "Banshee" },
{ "Demon", "Hydra", "Giant Spider" },
};
注意:'drawf' - > '侏儒'。
答案 2 :(得分:1)
正如错误消息告诉您的那样,您尝试初始化的数组不足以容纳这些字符串的所有字符。
例如,“Giant Spider”在包含终止空字符'\0'
时需要13个字符。
尝试以下方法:
char monsternivel1[4][3][13] = {
{"Rat","Bat","Spider"},
{"Goblin","Orc","Drawf"},
{"Dragon","Lich","Banshee"},
{"Demon","Hydra","Giant Spider"}
};
考虑这一点的方法是指针。不确定你是否熟悉它们,但C中的字符数组是作为指向数组中第一个字符的指针实现的。
因此,数组的第一个索引指定了指向字符指针数组指针的数量。
第二个指定每个指针数组中字符数组的指针数。
最后,第三个索引指定包含数据的最终数组中的最大字符数(长度)。
是否需要2-D指针数组或3-D字符数组取决于您计划对程序中其他位置的数据执行的操作。