I wrote a program for my assignment that involves pointers and dynamic allocation for an array, I am getting a runtime error and the program crashes, there are no compiling errors. Here is the program :
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;
}
Sample Output :
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
Crashes here
Can someone let me know what the problem is with the PrintArray function?