Click to See Complete Forum and Search --> : pointer access --0xC0000005: Access Violation


j94086
July 16th, 2005, 02:01 PM
When I run this one, I get access 0xC0000005: Access Violation..Can anybody tell me whats wrong with this code..?

I am trying to get the students grade into a pointer array and storing them .




#include <iostream.h>

int main () {

int const MAX = 15;

char *ftr[MAX];

cout << "Enter the grades of the student one by one" << endl;

for (int i=1; i<=10; i++)
{
cout << "Enter the Student Number" << endl;

cin.get(*(ftr), MAX);
}
return 0;

}

Bob Davis
July 16th, 2005, 02:07 PM
1. You are reading data into an uninitialized pointer; it is pointing to garbage. To do what you want, you're better off using std::string.

2. You included the old, deprecated header. Include <iostream> instead.


#include <iostream>
#include <string>

using namespace std;

int main ()
{

int const MAX = 15;
string ftr[MAX];

cout << "Enter the grades of the student one by one" << endl;

for (int i=0; i < MAX; i++)
{
cout << "Enter the Student Number" << endl;
getline(cout, ftr[i]);
}

return 0;
}

NoHero
July 16th, 2005, 02:08 PM
char *ftr[MAX];

Creates an array of pointers to character which point to nowhere. And not an array of characters.

char ftr[MAX];

Is what you need.

SuperKoko
July 16th, 2005, 02:11 PM
It is a memory access violation.
You accessed non allocated memory, because you forgot to allocate memory for each buffer:



#include <iostream.h>

int main () {

int const MAX = 15;
int const MAX_SIZE = 256;

char *ftr[MAX];
for(size_t i=0;i<MAX;++i)
ftr[i]=new char[MAX_SIZE];

cout << "Enter the grades of the student one by one" << endl;

for (int i=1; i<=10; i++)
{
cout << "Enter the Student Number" << endl;

cin.get(*(ftr+i), MAX_SIZE);
}
for(size_t i=0;i<MAX;++i)
delete[] ftr[i];
return 0;

}



But it is better (easier and safer) to use std::string



#include <iostream.h>
#include <string>

int main () {

int const MAX = 15;

std::string ftr[MAX];

cout << "Enter the grades of the student one by one" << endl;

for (int i=1; i<=10; i++)
{
cout << "Enter the Student Number" << endl;

cin>>ftr[i];
}

return 0;

}

j94086
July 16th, 2005, 02:23 PM
thanks guys!! appreciated.

RoboTact
July 16th, 2005, 04:36 PM
And in almost all cases it's better to use vector instead of array:#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main ()
{

vector<string> ftr(15);

cout << "Enter the grades of the student one by one" << endl;

for (int i=0; i < ftr.size(); i++)
{
cout << "Enter the Student Number" << endl;
getline(cout, ftr[i]);
}

return 0;
}

pumpkindyy
July 17th, 2005, 03:09 AM
When I run this one, I get access 0xC0000005: Access Violation..Can anybody tell me whats wrong with this code..?

I am trying to get the students grade into a pointer array and storing them .




#include <iostream.h>

int main () {

int const MAX = 15;

char *ftr[MAX];

cout << "Enter the grades of the student one by one" << endl;

for (int i=1; i<=10; i++)
{
cout << "Enter the Student Number" << endl;

cin.get(*(ftr), MAX);
}
return 0;

}

char *ftr[MAX] is an pointer array which not you expected.
char *ftr or char ftr[MAX] are both OK!

Good Luck!