首页 > 其他 > 详细

strcpy & memcpy区别

时间:2018-02-01 19:58:15      阅读:230      评论:0      收藏:0      [点我收藏+]

这两个经常使用的函数,主要区别有:

  1. strcpy 返回值是char *, strcpy(x1, x2); x1 x2必须都是char* 类型
  2. memcpy(x1, x2, sizeof(xx)); memcpy可以复制的类型很多;

如果你使用一个数组指针,则不能使用strcpy, 只能使用memcpy.

#cat aa.c
#include <stdio.h>
#include <string.h>

int main()
{
    char s1[64] = "hello";
    char (*ps1)[64] = &s1;

    printf("ps1:%s\n", ps1);
    char s2[64];

    char (*ps2)[64] = &s2;
    strcpy(ps2, ps1);
    memcpy(ps2, ps1, sizeof(char[64]));

    printf("s2:%s\n", s2);

    return 0;
}

strcpy & memcpy区别

原文:https://www.cnblogs.com/muahao/p/8400583.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!