Returning Pointers to Strings

Learn about the dangers of returning pointers to strings.

Returning pointers to strings

Let’s declare two strings in a function:

char* func()
{
    char str1[]   = "Hello";
    char str2[16] = "World";

    return str1; //return str2;
}

Both str1 and str2 are local variables of func. They get allocated when the function starts executing. Their stack frame is invalidated when the function ...