I am trying to write a custom function in C that will concatenate two strings. So far, I have come up with:
char *strcat406(char *str1, char *str2) {
    int str1length = 0;
    int str2length = 0;
    char newStr[str1length + str2length];
    int i;
    for (i = 0; i < str1[i] != '\0'; i++)
        str1length += 1;
    for (i = 0; i < str2[i] != '\0'; i++)
        str2length += 1;
    for (i = 0; i < str1[i] != '\0'; i++)
        newStr[i] = str1[i];
    for (i = i; i < str2[i] != '\0'; i++)
        newStr[i] = str2[i];
    return newStr;
}
I believe the code should work except for the line that reads return newStr;, Xcode is giving me an error that reads "address of stack memory associated with local variable (x) returned" and I think that is why I am not getting the string array printed in main like I want it to.
From my research, it seems like this is happening because the memory for the array returned is freed, and so I get a result I do not want, though I have not found a single answer on this site or even in the C documentation that has worked for me.
How can I change my function so that it returns the concatenation of two strings?
 
     
    