C++ 如何正确初始化多维字符数组并将其传递给函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5835170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to correctly initialize multidimentional char array and pass it to function?
提问by Alex
I'm writing a program 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?
VS C++ 给了我一条警告消息,说这样的数组的大小太小了。我想这是因为每行中也必须有 '\0' 符号。如何在没有 '\0' 符号的情况下初始化 char 数组?我不想size
用值 13进行初始化,因为将这个常量用于函数(打印数组、移动等)会很混乱,有什么办法可以做到吗?
Also, how to pass this array to function void mazeTraverse
using pointer?
另外,如何void mazeTraverse
使用指针将此数组传递给函数?
int main()
{
mazetraverse(maze)
}
void mazeTraverse(char (*maze)[ size ])
Such code doesn't works...
这样的代码不起作用...
回答by Mateen Ulhaq
You need to account for the NULL character at the end of the string:
您需要考虑字符串末尾的 NULL 字符:
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
?
我看到你在使用 C++。你有什么理由不使用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>
.
如果你更疯狂,你会使用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
. 它就像 a 一样工作,char*
但您不必手动分配它/等。例如:
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...
回答by SuperElectric
So long as you're using C++, why not just make a simple class?:
只要您使用 C++,为什么不创建一个简单的类呢?:
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":
您可以利用后续字符串文字(如“foo”“bar”)隐式连接到“foobar”这一事实轻松初始化它:
Maze my_maze(12,
"############"
"#...#......#"
"..#.#.####.#"
"###.#....#.#"
"#....###.#.."
"####.#.#.#.#"
"#..#.#.#.#.#"
"##.#.#.#.#.#"
"#........#.#"
"######.###.#"
"#......#...#"
"############");
回答by AnT
"Doesn't works"? The code doeswork. 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).
“没用”?该代码确实有效。它工作得很好。(假设编译器允许您使用那些 13 个字符的字符串文字来初始化大小为 12 的数组。这实际上是 C++ 中的一个错误,但您说您收到的只是警告)。
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 ])
回答by cellcortex
To initialize, I would:
要初始化,我会:
char* maze[ size ] = {
"############",
"#...#......#",
"..#.#.####.#",
"###.#....#.#",
"#....###.#..",
"####.#.#.#.#",
"#..#.#.#.#.#",
"##.#.#.#.#.#",
"#........#.#",
"######.###.#",
"#......#...#",
"############",
};
To pass parameters you should be able to use char**. So that would be:
要传递参数,您应该能够使用 char**。所以那将是:
void mazeTraverse(char ** param)