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

It says :

> 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]|

I didn't understand 3d arrays of char, any ideas? The first [] should be variable, the second [] should be how many strings and the third should be the categories?

Lucas Bertollo
  • 373
  • 2
  • 5
  • 19

3 Answers3

6

What you want to do it probably:

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

It's still an two dimensional array with char*'s. Notice, that the order for an 2 dimensional array is [row][column] and not [column][row],

Constantin
  • 8,721
  • 13
  • 75
  • 126
  • You mean `const char *`, surely. – paddy Nov 06 '13 at 22:26
  • so i dont need 3d array? – Lucas Bertollo Nov 06 '13 at 22:30
  • 1
    @LucasBertollo Yes, 2 dimensions are absolutly sufficient in this case. More dimensions would just make it more complex and less maintainable in my opinion. If you use char* as type of the array, one 'entry' is already an null-terminated string - so the dimensions are really just for the strings itself (no dimension for every char necessary). – Constantin Nov 06 '13 at 22:32
  • @LucasBertollo Well, whether this is a 3D array or not depends on the point of view; since the last dimension is `char *`, you can also index it as a “third dimension” (i.e., characters of the strings). – Arkku Nov 06 '13 at 22:34
  • Oh thanks one more thing what means const and that * before the variable name? – Lucas Bertollo Nov 06 '13 at 22:34
  • @Constantin There is a difference between char * and char[] you don't mention and it can go very wrong. -1 for not explaining the trade-offs here. – zubergu Nov 06 '13 at 22:34
  • @Arkku no it doesn't. It is not a 3D array, because `char*` data is not stored with array layout properties. It is a 2D array of pointers. – paddy Nov 06 '13 at 22:35
  • @paddy Like I said, it depends on the point of view. Often “high-dimension arrays” are constructed with pointers even in the second dimension. The resulting data structure is still generally called an array even though the memory layout is not equivalent to that of a true 3D array. – Arkku Nov 06 '13 at 22:39
3

If you want a 3D array, then the last dimension must be big enough for each string:

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

Note: 'drawf' -> 'dwarf'.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
1

As the error messages are telling you, the array you're trying to initialize isn't long enough to hold all the characters of those strings.

"Giant Spider" for instance, requires 13 characters, when including the terminating null character, '\0'.

Try the following:

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

The way to think about this is in terms of pointers. Not sure if you're familiar with them, but character arrays in C are implemented as pointers to the first character in the array.

Thus, the first index of your array specifies the number of pointers to arrays of pointers to characters.

The second specifies the number of pointers to character arrays in each pointer array.

Lastly, the third index specifies the max number of characters (length) in the final array containing the data.

Whether you want a 2-D array of pointers, or a 3-D array of chars depends on what you're planning to do with the data elsewhere in your program.

Max Wallace
  • 3,609
  • 31
  • 42