0

I wanna add second array to end of the first array.Can someone recover this code?

#include<stdio.h> char gift (char *sPtr1,char *sPtr2) { int i=0,k=0; while(sPtr1[i]!=NULL) { i++; } for(; sPtr2[k]=NULL; k++){ sPtr1[i+k+1]=sPtr2[k]; } } int main() { char string1[]="You can"; char string2[]=" go"; puts(string1); puts(string2); gift(string1,string2); printf("%s",string1); return 0; }

31st Dec 2018, 7:38 PM
Nurullah Aydın
Nurullah Aydın - avatar
5 ответов
+ 2
You can't modify the original strings. You must allocate a new storage location for the string. https://code.sololearn.com/cJ707PL3E108 Note: NULL is a pointer not a character so shouldn't be used in your code.
1st Jan 2019, 3:23 AM
John Wells
John Wells - avatar
+ 1
Thank you sir. I just don't know what is the mission of malloc at here . Could you explain for me ?
1st Jan 2019, 9:31 AM
Nurullah Aydın
Nurullah Aydın - avatar
+ 1
It allocates heap memory for your pointer to store the string (same as new in C++.) Normally, you would call free to return the memory, when done. But, your program exiting makes it unneccessary.
1st Jan 2019, 10:00 AM
John Wells
John Wells - avatar
+ 1
Thank you again for your help sir. Have a good day :))
1st Jan 2019, 11:18 AM
Nurullah Aydın
Nurullah Aydın - avatar
0
posted comment in the code java
1st Jan 2019, 10:14 AM
Ḿổḩảḿḿẻď Ấḿiň
Ḿổḩảḿḿẻď Ấḿiň - avatar