howa posted:
#include <iostream>
using namespace std;
const char* test(char *src) {
string s(src);
return s.c_str();
}
int main() {
char src[] = "test123";
cout<<test(src);
return 0;
};
how to get the "test123" from the function?
The memory which the string occupies must still be accessible after the
funciton returns. Therefore, either:
(1) Have the calling function allocate the memory, and pass a pointer so
that the called function will alter the memory.
(2) Have the called function allocate the memory dynamically, and return a
pointer which the calling function shall have to delete.
(3) Use global/static data...
--
Frederick Gotham