【发布时间】:2014-04-06 00:07:07
【问题描述】:
我为我的作业编写了一个程序,其中涉及数组的指针和动态分配,我收到运行时错误并且程序崩溃,没有编译错误。这是程序:
array.h:
#include <iostream>
using namespace std;
void readArray(float*, int &);
void PrintArray(float*, int);
array.cpp:
#include "array.h"
void readArray(float* array, int &size)
{
array = new float[size];
cout << endl;
cout << "Enter the array elements, use spaces: ";
for (int i = 0; i < size; i++)
{
cin >> array[i];
}
cout << endl;
}
void PrintArray(float * array, int size)
{
for (int i = 0; i < size; i++)
{
cout << array[i] << " ";
}
cout << endl;
}
main.cpp:
#include "array.h"
int main()
{
int size = 0;
cout << "How many elements would you like to enter? ";
cin >> size;
cout << endl;
float *array = NULL;
readArray(array,size);
cout << "The array size is " << size << endl;
PrintArray(array, size);
return 0;
}
样本输出:
How many elements would you like to enter? 3
Enter the array elements, use spaces: 4.0 5.0 6.0
The array size is 3
在这里崩溃
谁能告诉我 PrintArray 函数有什么问题?
【问题讨论】:
标签: c++ arrays pointers dynamic