C++ 如何从函数返回一个字符数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5660527/
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 do I return a char array from a function?
提问by Datoxalas
I've tried the following:
我尝试了以下方法:
char[10] testfunc()
{
char[10] str;
return str;
}
回答by Xeo
Best as an out parameter:
最好作为输出参数:
void testfunc(char* outStr){
char str[10];
for(int i=0; i < 10; ++i){
outStr[i] = str[i];
}
}
Called with
与
int main(){
char myStr[10];
testfunc(myStr);
// myStr is now filled
}
回答by Marijn van Vliet
You have to realize that char[10]
is similar to a char*
(see comment by @DarkDust). You are in fact returning a pointer. Now the pointer points to a variable (str
) which is destroyed as soon as you exit the function, so the pointer points to... nothing!
您必须意识到这char[10]
类似于char*
(请参阅@DarkDust 的评论)。你实际上是在返回一个指针。现在指针指向一个变量 ( str
),一旦退出函数,该变量就会被销毁,因此指针指向...什么都没有!
Usually in C, you explicitly allocate memory in this case, which won't be destroyed when the function ends:
通常在 C 中,你在这种情况下显式分配内存,当函数结束时它不会被销毁:
char* testfunc()
{
char* str = malloc(10 * sizeof(char));
return str;
}
Be aware though! The memory pointed at by str
is now NEVER destroyed. If you don't take care of this, you get something that is known as a 'memory leak'. Be sure to free()
the memory after you are done with it:
不过要注意!指向的内存str
现在永远不会被破坏。如果你不注意这一点,你会得到一些被称为“内存泄漏”的东西。free()
完成后一定要记住:
foo = testfunc();
// do something with your foo
free(foo);
回答by Flinsch
As you're using C++ you could use std::string
.
当您使用 C++ 时,您可以使用std::string
.
回答by Felice Pollano
a char array is returned by char*, but the function you wrote does not work because you are returning an automatic variable that disappear when the function exits. Use something like this:
char* 返回一个 char 数组,但您编写的函数不起作用,因为您返回的是一个自动变量,该变量在函数退出时消失。使用这样的东西:
char *testfunc() {
char* arr = malloc(100);
strcpy(arr,"xxxx");
return arr;
}
This of course if you are returning an array in the C sense, not an std:: or boost:: or something else. As noted in the comment section: remember to free the memory from the caller.
当然,如果您返回的是 C 意义上的数组,而不是 std:: 或 boost:: 或其他东西。如评论部分所述:记得从调用者那里释放内存。
回答by MSalters
With Boost:
带升压:
boost::array<char, 10> testfunc()
{
boost::array<char, 10> str;
return str;
}
A normal char[10]
(or any other array) can't be returned from a function.
char[10]
不能从函数返回普通(或任何其他数组)。
回答by devcodexyz
when you create local variables inside function that are created in stack most likely get overwritten in memory when exiting the function. so code like this in most c++ implementations will not work:
当您在函数内部创建在堆栈中创建的局部变量时,很可能在退出函数时在内存中被覆盖。所以在大多数 C++ 实现中这样的代码将不起作用:
char[] pupulateChar()
{
char* ch = "wonet return me";
return ch;
}
a fix is to create the variable that want to be populated outside the function or where you want to use it then pass it as parameter and manipulate function, example:
解决方法是创建要填充到函数外部或要使用它的地方的变量,然后将其作为参数传递并操作函数,例如:
void populateChar(char* ch){
strcpy(ch,"fill me will, this will stay",size); // this will work as long it won overflow it.
}
int main(){
char ch[100]; // reserve memory in stack outside the function
populateChar(ch); //populate array
}
c++11 solution using std::move(ch) to cast lvalues to rvalues
使用 std::move(ch) 将左值转换为右值的 c++11 解决方案
void populateChar(char* && fillme){
fillme = new char[20];
strcpy(fillme, "this worked for me");
}
int main(){
char* ch;
populateChar(std::move(ch));
return 0;
}
or this option in c++11:
或 c++11 中的此选项:
char* populateChar(){
char* ch = "test char";
// will change from lvalue to r value
return std::move(ch);
}
int main(){
char* ch = populateChar();
return 0;
}