const char* returnStr()
{
char time[40] = {"France"};
char* time1;
time1 = time;
return time1;
}
int main(int argc, char* argv[]) {
printf ("return String is %s\n",returnStr());
}
This code returns some junk characters. Won’t the const char* not enough to return the local char pointer? Do I have to use the static too in the function?
Yes. The
constis just a qualifier on the return value, signaling to callers ofreturnStrthat they shouldn’t modify the result of the function. It doesn’t changetime‘s temporary character.