Amit
asked on
can somebody tell me why this program throws the error - memory cannot be read.
If somebody finds the mistake, please tell me the reason also,so that I can learn the langauge.
void get(double * a, int &n)
{
cout<<"Enter: ";
cin>>n;
cout<<"___________________ _____"<<en dl;
a=new double[n];
for (int i=0;i<n;i++)
{
cout<<"Enter item "<<i+1<<":";
cin>>a[i];
}
}
void main()
{
int i;
double *game;
get(game,i);
for(int j=0;j<i;j++)
{
cout<<game[j]+5<<endl;
}
}
void get(double * a, int &n)
{
cout<<"Enter: ";
cin>>n;
cout<<"___________________
a=new double[n];
for (int i=0;i<n;i++)
{
cout<<"Enter item "<<i+1<<":";
cin>>a[i];
}
}
void main()
{
int i;
double *game;
get(game,i);
for(int j=0;j<i;j++)
{
cout<<game[j]+5<<endl;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you both.
What you need to do is either send a pointer to a pointer, double **a and then use *a = new double[n]; instead of the current code. Another alternative is to send a reference to the pointer instead of only a pointer.