memcpy(内存拷贝函数)简介

效果:实现内存拷贝

参数:第一个参数是一个指针,指向拷贝目标区域;第二个参数是一个指针,指向被拷贝的内存区域;第三个参数是一个数,指定拷贝内容的内存大小

函数原型:void *memcpy(void* str1, const void* str2, size_t n)

头文件:#include<string.h>

返回值:指向str1的指针(这里的str1即函数原型中的str1)

 1 #include <bits/stdc++.h> 
 2 
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     int str1[6];
 8     int str2[6];
 9     for(int i = 0; i < 6; i++){
10         str2[i] = i;
11     }
12     cout << "str2:";
13     for(int i = 0; i < 6; i++){
14         cout << " " << str2[i];
15     }
16     cout << endl;
17     memcpy(str1, str2, sizeof(str2));
18     cout << "str1:";
19     for(int i = 0; i < 6; i++){
20         cout << " " << str1[i];
21     }
22 }

运行结果为:

str2: 0 1 2 3 4 5
str1: 0 1 2 3 4 5

 

posted @ 2020-08-21 00:03  江南湖西北  阅读(6256)  评论(0编辑  收藏  举报