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
Post a Comment