error: function returns address of local variable

I’m beginner with C and I am learning on my own. I am creating the following function: I am basically trying to return an appended string, but I get the following error: “error: function returns address of local variable”, any suggestions, how to fix this?

Does C have a string type?

C does not and never has had a native string type. By convention, the language uses arrays of char terminated with a null char, i.e., with ‘\0′. Functions and macros in the language’s standard libraries provide support for the null-terminated character arrays, e.g., strlen iterates over an array of char until it encounters a ‘\0’ … Read more

Proper way to empty a C-String

It depends on what you mean by “empty”. If you just want a zero-length string, then your example will work. This will also work: If you want to zero the entire contents of the string, you can do it this way: but this will only work for zeroing up to the first NULL character. If … Read more

strcpy vs strdup

strcpy(ptr2, ptr1) is equivalent to while(*ptr2++ = *ptr1++) where as strdup is equivalent to (memcpy version might be more efficient) So if you want the string which you have copied to be used in another function (as it is created in heap section) you can use strdup, else strcpy is enough.

Difference between ‘strcpy’ and ‘strcpy_s’?

strcpy is a unsafe function. When you try to copy a string using strcpy() to a buffer which is not large enough to contain it, it will cause a buffer overflow. strcpy_s() is a security enhanced version of strcpy(). With strcpy_s you can specify the size of the destination buffer to avoid buffer overflows during copies.