How do I return a char array from a function?

后端 未结 6 1091
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 13:13

I\'ve tried the following:

char[10] testfunc()
{
    char[10] str;

    return str;
}
相关标签:
6条回答
  • 2020-11-28 13:42

    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!

    Usually in C, you explicitly allocate memory in this case, which won't be destroyed when the function ends:

    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:

    foo = testfunc();
    // do something with your foo
    free(foo); 
    
    0 讨论(0)
  • 2020-11-28 13:47

    As you're using C++ you could use std::string.

    0 讨论(0)
  • 2020-11-28 13:56

    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:

    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

    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:

    char* populateChar(){
        char* ch = "test char";
        // will change from lvalue to r value
        return std::move(ch);
    }
    int main(){
        char* ch = populateChar();
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-28 14:01

    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
    }
    
    0 讨论(0)
  • 2020-11-28 14:06

    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 *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.

    0 讨论(0)
  • 2020-11-28 14:08

    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.

    0 讨论(0)
提交回复
热议问题