0
Is it imposible to copy the data from pointing function?
I wanna copy data from function that returns pointer to the string, but i dont know how. Using strcpy(). https://code.sololearn.com/cUGYT1SG8j7H/?ref=app
4 ответов
+ 4
Yes, because you write memory which you have not allocated, in fact, name may point to memory you are *not allowed to* write to. Please read my answer again carefully, try to understand, and apply the changes I suggested.
+ 3
Samialloh I think you don't put func() in strcpy and your minor mistake is you don't use header file #include<string.h>
+ 3
If you use a function from the string library, then you ought to include that library:
#include <string.h>
The tricky thing about the usage of strcpy is that *you* have to make sure that the receiving memory area is large enough to hold what you want to copy into it. For security reasons, strncpy is the safer choice as it will limit the amount copied to at most n which you can set smaller than or equal to the memory allocated. Anyway, your memory for receiving a copy of the string is at present zero bytes. And therein lies the problem. You need to allocate memory before writing it. E.g.:
char name[32];
+ 1
Sakshi Ani Jona 🕊
My code outputs "Segmentation fault" in VIM
Help me to fix it.