Returning a string from a function in C

前端 未结 7 1107
耶瑟儿~
耶瑟儿~ 2020-12-18 07:28

I have a c function that I want to return a string.

If I print the string before it is returned then I see croc_data_0186.idx

If I try and print the string

7条回答
  •  感动是毒
    2020-12-18 07:48

    You are returning a pointer to a local variable that doesn't exist anymore when the function returns. You have to malloc storage for it and return that. Alternatively, you can let the caller pass in a buffer to be filled. In any case the caller is responsible for freeing the memory later.

提交回复
热议问题