I'm perplexed as to why the following doesn't work:
char * f = "abcdef";
strcpy(f, "abcdef");
printf("%s",f);
char s[] = "ddd";
strcpy(&s[0], "eee");
printf("%s", s);
In both examples strcpy received a char * yet on the first example it dies a horrible death.
"abcdef"
and "ddd"
are string literals which may reside in a read-only section of your address space. char s[] = "ddd"
ensures this literal is copied to stack - so it's modifiable.
char * f = "abcdef";
defines a char pointer to "abcdef" which is located in read-only area so you can't write to this place
char s[] = "ddd";
defines a char array on the stack which is writable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With