当前位置: 移动技术网 > IT编程>开发语言>C/C++ > memcpy内存拷贝函数的写法c++代码实例及运行结果(代码教程)

memcpy内存拷贝函数的写法c++代码实例及运行结果(代码教程)

2018年02月06日  | 移动技术网IT编程  | 我要评论

儋州政府,郑州汽车站时刻表,公安部副部长刘德

memcpy()为内存拷贝函数,相比于strcpy只能拷贝字符串,memcpy可以拷贝任意类型的数据

下面用c++写一个跟memcpy()函数相同功能的函数

c++代码

#include <iostream>  
#include<assert.h>  
using namespace std;  
  
void *memCpy(char *dst,char *src,size_t size)  
{  
    assert(dst!=NULL&&src!=NULL);  
    char *Dst=dst;//防止改变dst的地址  
    char *Src=src;  
    while(size-->0)  
        *Dst++=*Src++;  
    return dst;  
}  
  
int main()  
{  
    char src[]="hello world";  
    char dst[20];  
    memCpy(dst,src,11);  
    cout<<"源字符串为"<<src<<endl;  
    cout<<"复制后字符串为"<<dst<<endl;  
    return 0;  
}  

运行结果

\

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网