美文网首页
字符串函数:字符串函数strcpy

字符串函数:字符串函数strcpy

作者: 爱生活_更爱挺自己 | 来源:发表于2020-11-05 18:14 被阅读0次

strcpy

  • char * strcpy(char *restrict dst, const char *restrict src);

  • 把src的字符串拷贝到dst

    • restrict标明src和dst不重叠(C99)
  • 返回dst

    • 为了能链起代码来

复制一个字符串

char *dst = (char *)malloc(strlen(src)+1);

strcpy(dst, src);

#inlcude<stdio.h>
#include<string.h>

//数组版本
char* mycpy(char *dst, const char* src)
{
    int idx = 0;
    while (src[idx] != '\0'){
        dst[ids]=src[idx];
        idx++;
    }
    dst[idx] = '\0';
    return dst;
}

int main(int argc, char const *argv[])
{
    char s1[] = "abc";
    char s2[] = "abc";
    strcpy(s1,s2);
    
    return 0;
}
#inlcude<stdio.h>
#include<string.h>

//数组版本
char* mycpy(char *dst, const char* src)
{
    char* ret = dst;
   while (*src != '\0'){
       //*dst = *src;
       //dst++;
       //src++;
       *dst++ = *src++;
   }
    *dst = '\0';
    return ret;
}

int main(int argc, char const *argv[])
{
    char s1[] = "abc";
    char s2[] = "abc";
    strcpy(s1,s2);
    
    return 0;
}

相关文章

网友评论

      本文标题:字符串函数:字符串函数strcpy

      本文链接:https://www.haomeiwen.com/subject/ngsavktx.html