52

I've tried the following:

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

    return str;
}
4
  • 2
    So, you have tried one thing, it didn't work, and now you ask? Have you used the search function? Have you looked at the suggestions made when you asked the question? Have you looked at the Related list on the right side of this page? Questions like this have been asked a bajillion times here on SO, so with minimal effort, you should have been able to find a solution. Commented Apr 14, 2011 at 8:35
  • 1
    This question has been asked (and answered) many times. Take a look at: how to return an array in a c method Commented Apr 14, 2011 at 8:36
  • See also: C FAQ Chapter 19: Returning arrays. Commented Apr 14, 2011 at 8:46
  • 3
    Reopened because the alleged duplicate was not, and the answer to that not applicable here. Commented Jan 12, 2018 at 3:14

7 Answers 7

73

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

Please also pass the capacity as argument, it's too fragile this way.
Also, a strncpy or memcpy would be more efficient when copying out the result.
32

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); 

2 Comments

A char array is not the same as a char pointer. See the C FAQ question 6.2.
to be fair Marjin said 'similar'. From the linked FAQ "Arrays are not pointers, though they are closely related (see question 6.3) and can be used similarly." which is basically what Marjin said.
14

A char array is returned by char*, but the function you wrote does not work because you are returning an automatic variable that disappears when the function exits.

Use something like this:

char *testfunc() {
    char* arr = malloc(100);
    strcpy(arr,"xxxx");
    return arr;
}

This is 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.

4 Comments

Sorry Felice, but you haven't understood my point; the memory leak is not in the assignment (and strcpy does not help at all, either). It's that you are reserving memory via malloc, but no one is deallocating that memory (calling free() in this case). You should note that, in this case, the caller function should check if the poiner is not null, and then free() it after it's used. Otherwise, you are introducing a memory leak. Anyway, it's a bad practice and should be avoided like hell.
@mydaemon well I can't write all the code of the entire program, it is clear that the OP has to free the memory after its usage. As you pointed off, the code before the edit was wrong, because it will cause the loss of the allocated pointer.
I do agree with the change, but as an advice, never assume the op will know the same things than you. It takes literally 30 seconds to add a note: you should be calling free from the caller function".
As stated here, you need to cast the return of malloc().
9

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

1 Comment

As you are answering the question, you could stay on topic. User asked how does one return an char array from a function, not what should s/he use. The reasoning why exactly char array is unknown. It could be a practice, it could be a homework, it could be looking under the hood... best not to assume.
4

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.

2 Comments

This does a copy on exit, rather than pass a reference. May not be a problem but for large arrays this could be a substantial cost. However with use of the return value optimisation (en.wikipedia.org/wiki/Return_value_optimization) you could completely elide the copy.
Instead of boost::array, one can also use std::array and avoid the dependency on boost.
1

When you create local variables inside a function that are created on the stack, they most likely get overwritten in memory when exiting the function.

So code like this in most C++ implementations will not work:

char[] populateChar()
{
    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, and then pass it as a parameter and manipulate the function, example:

void populateChar(char* ch){
    strcpy(ch, "fill me, Will. This will stay", size); // This will work as long as it won't overflow it.
}

int main(){
    char ch[100]; // Reserve memory on the stack outside the function
    populateChar(ch); // Populate the array
}

A 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;
}

Comments

0

With c++17 you can use next code:

auto testfunc() {
    union {
        char str[14];
    } res = { .str = "Hello, World!" };
    return res;
}

and then use you string as

const auto str = testfunc();
std::cout << str.str;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.