C++ 如何返回二维字符数组C++?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/720707/
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 return two dimensional char array c++?
提问by r4ccoon
i ve created two dimensional array inside a function, i want to return that array, and pass it somewhere to other function..
我在函数内部创建了二维数组,我想返回该数组,并将其传递给其他函数。
char *createBoard( ){
char board[16][10];
int j =0;int i = 0;
for(i=0; i<16;i++){
for( j=0;j<10;j++){
board[i][j]=(char)201;
}
}
return board;
}
but this keeps giving me error
但这一直给我错误
回答by 1800 INFORMATION
Yeah see what you are doing there is returning a pointer to a object (the array called board
) which was created on the stack. The array is destroyed when it goes out of scope so the pointer is no longer pointing to any valid object (a dangling pointer).
是的,看看你在做什么,返回一个指向board
在堆栈上创建的对象(称为数组)的指针。数组在超出范围时被销毁,因此指针不再指向任何有效对象(悬空指针)。
You need to make sure that the array is allocated on the heap instead, using new
. The sanctified method to create a dynamically allocated array in modern C++ is to use something like the std::vector
class, although that's more complicated here since you are trying to create a 2D array.
您需要使用new
. 在现代 C++ 中创建动态分配数组的神圣方法是使用类似std::vector
类的东西,尽管这里更复杂,因为您要创建一个二维数组。
char **createBoard()
{
char **board=new char*[16];
for (int i=0; i<16; i++)
{
board[i] = new char[10];
for (int j=0; j<10; j++)
board[i][j]=(char)201;
}
return board;
}
void freeBoard(char **board)
{
for (int i=0; i<16; i++)
delete [] board[i];
delete [] board;
}
回答by 1800 INFORMATION
The best approach is create a board class and make the ctreateBoard function its constructor:
最好的方法是创建一个 board 类并使 ctreateBoard 函数成为它的构造函数:
class Board {
private:
char mSquares[16][10];
public:
Board() {
for(int i=0; i<16;i++){
for( int j=0;j<10;j++){
mSquares[i][j]=201;
}
}
// suitable member functions here
};
For information on how to use such a class, there is no substitute for reading a good book. I strongly recommend Accelerated C++by Andrew Koenig and Barbra Moo.
有关如何使用此类课程的信息,阅读一本好书是无可替代的。我强烈推荐Andrew Koenig 和 Barbra Moo 的Accelerated C++。
回答by Anders Hansson
I would really recommend using STL vector<> or boost/multi_array containers for this.
我真的建议为此使用 STL vector<> 或 boost/multi_array 容器。
If you must use arrays, then I would recommend using a typedef to define the array.
如果您必须使用数组,那么我建议使用 typedef 来定义数组。
typedef char[16][10] TBoard;
You could also return
你也可以返回
char**
...but then you would need to typecast it to the correct size in order to index it correctly. C++ does not support dynamic multiple dimension arrays.
...但是您需要将其类型转换为正确的大小才能正确索引它。C++ 不支持动态多维数组。
Also as others have suggested you can't return an object on the stack (i.e., local variable)
也正如其他人所建议的那样,您不能在堆栈上返回对象(即局部变量)
回答by sharptooth
This approach will not work. If you return a pointer to a local variable you'll run into undefined behaviour. Instead allocate an array on heap with newand copy data into it manually indexing it.
这种方法是行不通的。如果您返回一个指向局部变量的指针,您将遇到未定义的行为。而是在堆上使用new分配一个数组并将数据复制到其中手动索引它。
回答by Donotalo
Don't return pointer to a local variable, as other mentioned. If I were forced to do what you want to achieve, first I'd go for std::vector. Since you haven't learnt std::vector, here is another way:
不要像其他人提到的那样返回指向局部变量的指针。如果我被迫做你想做的事,首先我会选择 std::vector。因为你还没有学过 std::vector,这里是另一种方式:
void createBoard(char board[16][10])
{
int j =0;int i = 0;
for(i=0; i<16;i++){
for( j=0;j<10;j++){
board[i][j]=(char)201;
}
}
}
回答by Ingo
You must not return a pointer to a functions local variables because this space gets overwritten as soon as the function returns.
您不能返回指向函数局部变量的指针,因为一旦函数返回,该空间就会被覆盖。
The storage associated with board is on the function's stack.
与板相关的存储位于函数的堆栈中。
回答by DaClown
You should return char**
instead of char*
你应该返回char**
而不是char*
回答by Beno?t
The simple answer to your question is char**.
您问题的简单答案是 char**。
Having said that, DON'T DO IT ! Your "board" variable won't last outside createBoard().
话虽如此,不要这样做!您的“板”变量不会在 createBoard() 之外持续。
Use boost::multi_arrayand pass it as a reference to createBoard() or return it directly (but if you do that, it will be copied).
使用boost::multi_array并将其作为引用传递给 createBoard() 或直接返回它(但如果这样做,它将被复制)。