I came across lot of functions returning char pointers in one legacy application. Some of them returning pointers to local character arrays. It seems to be causing crashes a
No, you see buff[20]
is only available inside the f1
function. To be precise, the memory is allocated on f1
s stack.
You need to create a new buff[20]
on the heap using malloc
and return a pointer to that memory from inside f1
. Another way is to create buff[20]
outside f1 (from the function which calls f1
) and send it as an argument to f1
. f1
can then change the contents of the buffer and doesn't even have to return it.