64,639
社区成员
发帖
与我相关
我的任务
分享
//你题目有一些问题,把数组和指针没有区别开来,下面的程序也没有区别,但是你要记住,千万不要把数组和指针搞混了.
#include <iostream>
using namespace std;
int* AllocArray(int n)
{
return new int[n];
}
void FreeArray(int* const arr)
{
delete[] arr;
}
void ShowArray(const int* arr, int n)
{
for (int i = 0; i < n; ++i)
cout << arr[i] << '\t';
cout << endl;
}
void CopyArray(int* dest, const int* src, int n)
{
while (n--) *dest++=*src++;//用内存拷贝函数也行
}
int main(int argc, char* argv[])
{
int arr1[5] ={1,2,3,4,5};
const int size = sizeof(arr1)/sizeof(arr1[0]);
int* arr2 = AllocArray(size);
CopyArray(arr2, arr1, size);
ShowArray(arr2, size);
FreeArray(arr2);
return 0;
}