在c语言的库函数中,有一个函数实现了数组中元素的选择性拷贝———memcpy;
除了利用库函数之外,还可以通过自己的模拟实现元素的拷贝,先通过传到调用函数的值来选择要拷贝多少个元素;然后将所得到的数组利用循环打印在屏幕上,完成拷贝。
#include<stdio.h>
int *my_memcpy(int *crt,const int *tat, int cat)
{
int i = 0;
for (i = 0; i <cat;i++)
{
*crt = *tat;
crt++;
tat++;
}
return crt;
}
int main()
{
int arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
int arr2[10] = { 0 };
int sz = 0;
int i = 0;
scanf_s("%d", &sz);
my_memcpy(arr2, arr1, sz);
for (i = 0; i <sz; i++)
{
printf("%d ",arr2[i]);
}
system("pause");
return 0;
}