I want to ask about strcpy. I got problem here. Here is my code:
char *string1 = "Sentence 1";
char *string2 = "A";
strcpy(string1, string2);
I think there is no problem in my code there. The address of the first character in string1 and string2 are sent to the function strcpy. There should be no problem in this code, right?
Anybody please help me solve this problem or explain to me..
Thank you.
There is a problem — your pointers are each pointing to memory which you cannot write to; they’re pointing to constants which the compiler builds into your application.
You need to allocate space in writable memory (the stack via
char string1[<size>];for example, or the heap viachar *string1 = malloc(<size>);). Be sure to replace with the amount of buffer space you need, and add an extra byte at least for NULL termination. If youmalloc(), be sure youfree()later!