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; }
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.
+ 1
Thank you sir. I just don't know what is the mission of malloc at here . Could you explain for me ?
+ 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.
+ 1
Thank you again for your help sir. Have a good day :))
0
posted comment in the code java