strcpy using pointers

前端 未结 7 1064
无人及你
无人及你 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:07

    I don't see any need to pass a pointer-to-pointer:

    void str_cpy(char *dst, const char *src) {
       while (*src != '\0') {
          *dst++ = *src++; 
       }
       *dst = '\0';
    }
    

    And you need to allocate memory for dst before passing:

    const char *src = "String";
    char *str = malloc(strlen(src)+1); //plus one for null byte
    str_cpy(dst, src);
    

提交回复
热议问题