#include <iostream>
using namespace std;
const char* foo() {
return "Hello";
}
const char* bar() {
const char* s = "world";
return s;
}
void print(const char* s) {
cout << s << endl;
}
int main () {
cout << foo() << endl;
print(bar());
return 0;
}
Can I return those strings in foo and bar functions that way? that I ask this question is because I think those char* pointers in foo and bar functions are temporaries, and when the function calls in main return, they are deallocated, and I think the program should crash, but it compiles and runs fine. I have asked a similar question in an IRC channel, they mentioned static storage duration, I have googled for quite a while and found little on this. So what is static storage duration?
Your help will be greatly appreciated!