char* stringReturn()
{
char a[] = "Array of characters";
//return a; // I know stack allocation should not be returned
char *b = "Pointer to a string";
return b; // Is it safe ?
}
int main() {
char *str = stringReturn ();
cout<< str;
return 0; }
This is safe means then where the data “Pointer to a string” will be stored in the memory.
Yes, it is safe to return the value of
b. The string literal to whichbpoints has static storage duration.But, you must declare your pointers
const-correctly.bmust beconst char*b.