char array as a function returned value

how to have a temporary char array in a function as its returned value?

like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
include namespace std;
char* nFunction(string& s){
    const char* c=s.c_str();
     char cn[9];
     cn[0]=c[0];
     cn[1]=c[1];
     cn[2]=c[2];
    return cn;

}
int main(){

string s("foo bar");
cout<<"return= "<<nFunction(s) ;
}

not work... what's the correct?
Thanks.
Last edited on
There probably is a better way to do this, but here's some Frankensteined code that seems to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <cstring> // for strcpy_s

char* return_c_string(const std::string&);

int main()
{
   std::string str { "This is a string" };

   char cstr[100];

   strcpy_s(cstr, 100, return_c_string(str));

   std::cout << str << '\n' << cstr << '\n';
}

char* return_c_string(const std::string& str)
{
   char cstr[100] { };

   strcpy_s(cstr, 100, str.c_str());

   return cstr;
}

This is a string
This is a string

I honestly can't understand why you would want to do this. Just deal with a std::string until you need a C string, usually as a 3rd party library function parameter.
Last edited on
Compilers would (should) warn us about this (logical) error:

1
2
3
4
5
6
7
8
9
10
11
char* return_c_string( const std::string& )
{
   char cstr[100] { };

   // ...
   
   // GCC: warning: address of local variable 'cstr' returned
   // LLVM: warning: address of stack memory associated with local variable 'cstr' returned
   // Microsoft: warning: returning address of local variable or temporary
   return cstr;
}

http://coliru.stacked-crooked.com/a/bcf583c62f0d8af1
https://rextester.com/QHZUE1453
I did say there were probably better ways to do this, maybe allocate the C string on the heap?

Doesn't that possibly create a potential memory leak passing the C string back to main as a pointer?

C strings no matter what can be messy things to deal with IMO, especially when tossing them back and forth between functions.

"I'm against them."

https://www.youtube.com/watch?v=xHash5takWU

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

char* nFunction( string s )
{
   char *result = new char[s.size()+1];
   strcpy( result, s.c_str() );
   return result;
}

int main()
{
   string s{ "foo bar" };
   char *cs = nFunction( s );
   cout << "return = " << cs << '\n';
   delete [] cs;
}
Okay, so you return a pointer from a function.

Does it point to something allocated on the heap? Who is supposed to delete it?
Does it point to some file-scope or static memory? How long will it remain valid? If I call the function a second time with different variables, the value will change, right? If it's a pointer to some class's member data then it will disappear when the class instance is destroyed.

If your function returns a pointer then your comments need to make these issues absolutely clear. How long is the data valid? Is the caller responsible for deleting it?

For all of these reasons, it's usually a bad idea to return a pointer.

In your specific case, you could return a string instead of pointer.
Topic archived. No new replies allowed.