c++ - What's wrong with my strcpy? -


i tried make strcpy myself. should work, copied , pasted (almost exact code) someones post here strcpy. both give me "segmentation fault".

char* strcpy(char * destination, const char * source) {     while( (*destination++ = *source++) != '\0' )          ;     return destination; } 

what's wrong code?

char* = "hello"; cout << strcpy(a, "haha") << endl; 

you trying write data segment since "hello" stored there.

therefore, when call strcpy segmentation fault.

try:

char a[] = "hello"; cout << strcpy(a, "haha") << endl; 

instead.

edit: inside strcpy function, after copy, destination point end of string, need return beginning of string instead.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -