Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C 3d Array of char

Tags:

arrays

c

    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?

like image 749
Lucas Bertollo Avatar asked Sep 01 '25 01:09

Lucas Bertollo


2 Answers

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

like image 158
Constantin Avatar answered Sep 03 '25 00:09

Constantin


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'.

like image 42
Jonathan Leffler Avatar answered Sep 03 '25 01:09

Jonathan Leffler