|
-
July 16th, 2005, 02:01 PM
#1
pointer access --0xC0000005: Access Violation
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;
}
-
July 16th, 2005, 02:07 PM
#2
Re: pointer access --0xC0000005: Access Violation
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.
Code:
#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;
}
-
July 16th, 2005, 02:08 PM
#3
Re: pointer access --0xC0000005: Access Violation
Creates an array of pointers to character which point to nowhere. And not an array of characters.
Is what you need.
-
July 16th, 2005, 02:11 PM
#4
Re: pointer access --0xC0000005: Access Violation
It is a memory access violation.
You accessed non allocated memory, because you forgot to allocate memory for each buffer:
Code:
#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
Code:
#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;
}
-
July 16th, 2005, 02:23 PM
#5
Re: pointer access --0xC0000005: Access Violation
thanks guys!! appreciated.
-
July 16th, 2005, 04:36 PM
#6
Re: pointer access --0xC0000005: Access Violation
And in almost all cases it's better to use vector instead of array:
Code:
#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;
}
"Programs must be written for people to read, and only incidentally for machines to execute."
-
July 17th, 2005, 03:09 AM
#7
Re: pointer access --0xC0000005: Access Violation
 Originally Posted by j94086
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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|