Is it imposible to copy the data from pointing function? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

13th Apr 2023, 4:43 AM
Samuel Hayden
Samuel Hayden - avatar
4 Answers
+ 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.
13th Apr 2023, 5:37 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 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>
13th Apr 2023, 5:24 AM
Sakshi
Sakshi - avatar
+ 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];
13th Apr 2023, 5:27 AM
Ani Jona 🕊
Ani Jona 🕊 - avatar
+ 1
Sakshi Ani Jona 🕊 My code outputs "Segmentation fault" in VIM Help me to fix it.
13th Apr 2023, 5:34 AM
Samuel Hayden
Samuel Hayden - avatar