strcpy using pointers

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

    Recently I faced same problem of above one using double pointer strcpy implementation

    It might helpful to others below code

     void strcpy_i( char **dst, const char *src )
     {
        *dst=(char *)malloc((strlen(src)+1)*sizeof(char));
    
        char *tmp=*dst;
    
        if(tmp == NULL || src == NULL)
        return ;
    
        while((*tmp++=*src++)!='\0');
    }
    
    int main()
    {
        char v[]="Vinay Hunachyal";
        char *d=NULL;
    
        strcpy_i(&d,v);
        printf("%s",d);
    
     return 0;
    

    }

提交回复
热议问题