我一直在努力解决这个问题一直持续一周,我一直遇到问题.
我的目标:
编写一个为整数数组分配内存的函数.该函数将整数指针,数组大小和要分配的newSize作为参数.该函数返回指向已分配缓冲区的指针.首次调用该函数时,大小将为零,并将创建一个新数组.如果在数组大小大于零时调用该函数,则将创建一个新数组,并将旧数组的内容复制到新数组中.您的讲师已提供arrayBuilder.cpp作为此编程挑战的入门代码.此外,Lab9_1.exe是此应用程序的可执行文件,您可以测试它.
代码:
#include <iostream>
using namespace std;
int * arrayBuilder(int * arr, int size, int newSize);
void showArray(int * arr, int size);
int main()
{
int * theArray = 0;
int i;
cout << "This program demonstrates an array builder function." << endl << endl;
// create the initial array. The initial size is zero and the requested size is 5.
theArray = arrayBuilder(theArray, 0, 5);
// show the array before values are added
cout << "theArray after …Run Code Online (Sandbox Code Playgroud)