How to correctly initialize multidimentional char array and pass it to function?
I'm writing a program 开发者_C百科which finds exit from maze. I have a multidimentinal array representing the actual maze.
const int size = 12;
char maze[ size ][ size ] = {
"############",
"#...#......#",
"..#.#.####.#",
"###.#....#.#",
"#....###.#..",
"####.#.#.#.#",
"#..#.#.#.#.#",
"##.#.#.#.#.#",
"#........#.#",
"######.###.#",
"#......#...#",
"############",
};
VS C++ gives me a warning message, saying that size is too small for such array. I guess it's because there must be also '\0' symbol in each line. How do I initialize char array without '\0' symbols? I don't want to initialize size
with value 13 because it's will be too confused to use this constant for functions (printing array, making move etc.) Is there any way to do it?
Also, how to pass this array to function void mazeTraverse
using pointer?
int main()
{
mazetraverse(maze)
}
void mazeTraverse(char (*maze)[ size ])
Such code doesn't works...
You need to account for the NULL character at the end of the string:
char maze[size][size + 1] = { /* */ };
Alternatively, for more flexibility, you can do:
char *maze[size] = { /* */ };
I see you're using C++. Is there any reason you're not using std::string
?
std::string maze[size] = { /* */ };
It's a lot more flexible; now you just change the prototype to:
void mazeTraverse(std::string maze[]);
If you're even more insane, you'll use std::vector<std::string>
.
EDIT: I recommend learning a bit about std::string
. It works just like a char*
but you don't have to manually allocate it/etc. For example:
std::string mystring = "lol";
mystring = "lololol"; // perfectly legal!
std::cout << mystring[0] << "\n";
// Or: printf("%c\n", mystring[0]);
char* sz[8];
strcpy(sz, mystring[0].c_str());
// And so on...
So long as you're using C++, why not just make a simple class?:
class Maze {
public:
Maze(int width, const std::string& data)
:width_(width),
data_(data.begin(), data.end()) {
}
char operator()(int row, int column) const {
return data_[width_*row + column];
}
private:
int width_;
std::vector<char> data_;
};
You can initialize it easily by taking advantage of the fact that subsequent string literals, like "foo" "bar", are implicitly concatenated into "foobar":
Maze my_maze(12,
"############"
"#...#......#"
"..#.#.####.#"
"###.#....#.#"
"#....###.#.."
"####.#.#.#.#"
"#..#.#.#.#.#"
"##.#.#.#.#.#"
"#........#.#"
"######.###.#"
"#......#...#"
"############");
To initialize, I would:
char* maze[ size ] = {
"############",
"#...#......#",
"..#.#.####.#",
"###.#....#.#",
"#....###.#..",
"####.#.#.#.#",
"#..#.#.#.#.#",
"##.#.#.#.#.#",
"#........#.#",
"######.###.#",
"#......#...#",
"############",
};
To pass parameters you should be able to use char**. So that would be:
void mazeTraverse(char ** param)
"Doesn't works"? The code does work. And it works perfectly fine. (Assuming the compiler lets you to use those 13-character string literals to initialize arrays of size 12. This is actually an error in C++, but you said that you are getting a mere warning).
This
mazeTraverse(maze);
will compile and do exactly what you want it to do (the way I understand it). What exactly doesn't work in your case? "Doesn't work" is not exactly a meaningful description of the problem.
As for getting rid of the warning in the array initialization, if you insist on having the array of exact size, you'll have to initialize it in per-character fashion as in
char maze[ size ][ size ] = {
{ '#', '#', '#', ... },
{ ... },
// and so on
};
If you want to use string literals, then as you noted yourself, you have to declare the inner sub-arrays with bigger size
char maze[ size ][ size + 1 ] = {
"############",
// and so on
};
and change the function declaration accordingly
void mazeTraverse(char (*maze)[ size + 1 ])
精彩评论