I have written a function to accept user input of elements for a size of array, defined by user input. When I pass the address of the newly created array to the function and input numbers, I receive the following
"Exception thrown at 0x79845482 (msvcp140d.dll) in 5.11_Exercises_5.4.exe: 0xC0000005: Access violation writing "
#include <iostream>
#include <cmath>
usingnamespace std;
//double DataMinusMean(double*[], double*);
void DataEntry(int, double* []);
void DisplayData(double* [], int);
/*Function to calculate the sum of x - x_mean
double DataMinusMean(double* x[], double* x_mean)
{
}
*/
//Function for data entry
void DataEntry(int x, double* DataArray[])
{
for (int i = 0; i < x; i++)
{
cout << "What number would you like to enter? " << endl;
cin >> *DataArray[i];
}
cout << "Data Entry Complete" << endl;
}
void DisplayData(double* DataArray[], int j)
{
for (int i = 0; i <= j; i++)
{
cout << *DataArray[i] << endl;
}
}
int main()
{
int n = 0;
cout << "How many numbers in your data set?" << endl;
cin >> n;
cout << "The total of numbers in your data set is " << n << endl;
double* x = newdouble[n];
DataEntry(n, &x);
DisplayData(&x, n);
return 0;
}
And inside the function, as Ganado said, use DataArray[i] to access the array. The "[i]" part will increment the array the proper amount from the address sent to the function.
If I have something wrong due to misinterpretation some one let me know.
While that is true, x was already a pointer before being passed to a function, in this particular case.
Am I correct to say that just the address of the variable is passed to the function, not the variable itself?
In your original code, you were passing &x, which is the address of x.
In my/Handy Andy's suggestion, you are passing x itself, not &x.
So you are now passing the variable itself, but the variable itself is a pointer, which points to an array in dynamic memory.