c++ - How to correctly initialize multidimentional char array and pass it to function? -


i'm writing program finds exit maze. have multidimentinal array representing actual maze.

const int size = 12;     char maze[ size ][ size ] = {             "############",             "#...#......#",             "..#.#.####.#",             "###.#....#.#",             "#....###.#..",             "####.#.#.#.#",             "#..#.#.#.#.#",             "##.#.#.#.#.#",             "#........#.#",             "######.###.#",             "#......#...#",             "############",         }; 

vs c++ gives me warning message, saying size small such array. guess it's because there must '\0' symbol in each line. how initialize char array without '\0' symbols? don't want initialize size value 13 because it's confused use constant functions (printing array, making move etc.) there way it?

also, how pass array function void mazetraverse using pointer?

int main() {   mazetraverse(maze) }  void mazetraverse(char (*maze)[ size ]) 

such code doesn't works...

you need account null character @ end of string:

char maze[size][size + 1] = { /*  */ }; 

alternatively, more flexibility, can do:

char *maze[size] = { /*  */ }; 

i see you're using c++. there reason you're not using std::string?

std::string maze[size] = { /*  */ }; 

it's lot more flexible; change prototype to:

void mazetraverse(std::string maze[]); 

if you're more insane, you'll use std::vector<std::string>.


edit: recommend learning bit std::string. works char* don't have manually allocate it/etc. example:

std::string mystring = "lol"; mystring = "lololol"; // legal!  std::cout << mystring[0] << "\n"; // or: printf("%c\n", mystring[0]);  char* sz[8]; strcpy(sz, mystring[0].c_str());  // , on... 

Comments