What is the problem in that function?
char printThis()
{
char str[20] = "This is a string";
return str;
}
I get a warning : Incompatible pointer to integer conversion returning 'char[20]' from a function with result type char
Three problems that I can see:
char* but the function is declared to return char. The latter is just a single character rather than a string.void.Fix like this:
char* printThis(void)
{
char* str = strdup("This is a string");
return str;
}
Here I am using strdup to allocate the string on the heap. If you really wanted a buffer that was 20 characters long then you would need to use malloc() followed by strcpy(). Either way the responsibility for freeing the returned string is passed onto the caller.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With