As a follow-up to this question:
From what I\'ve seen, this should work as expected:
void greet(){
char c[] = \"Hello\";
greetWith(c);
return;
You can use either of these:
char const* getIt() {
return "hello";
}
char * getIt() {
static char thing[] = "hello";
return thing;
}
char * getIt() {
char str[] = "hello";
char * thing = new char[sizeof str];
std::strcpy(thing, str);
return thing;
}
shared_array getIt() {
char str[] = "hello";
shared_array thing(new char[sizeof str]);
std::strcpy(thing.get(), str);
return thing;
}
The first requires you to not write to the returned string, but also is the simplest you can get. The last uses shared_array which automatically can clean up memory if the reference to the memory is lost (the last shared_array to it goes out of scope). The last and second last must be used if you require a new string everytime you call the function.