strcpy using pointers

前端 未结 7 1070
无人及你
无人及你 2021-01-13 11:29

I\'m trying to write strcpy on my own using pointers and I get an error during runtime.

void str_cpy(char **destination, const char *source) {
//    char *s1         


        
7条回答
  •  天涯浪人
    2021-01-13 12:19

    Here is a complete implementation. Good article from here. Describes timing and performance. I did not measure myself though. http://www.howstuffworks.com/c35.htm

    char* mystrcpy(char *dst, const char *src) {
    char *ptr = dst;
    while ((*dst++ = *src++) ) ;
    return ptr;
    }
    
    int main(int argc, char *argv[]) {
    const char *src = "This is C.\0";
    char *dst = malloc(sizeof(char)*(strlen(src)+1)); //+1 for the null character
    dst = mystrcpy(dst, src);
    printf("%s",dst);
    return 1;
    }
    

提交回复
热议问题