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

jquery - Common JavaScript snippet to share files on Google Drive, Dropbox, Box.net or SkyDrive -

user interface - Python attempting to create a simple gui, getting "AttributeError: 'MainMenu' object has no attribute 'intro_screen'" -

Android Gson.fromJson error -