CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2005
    Posts
    2

    Exclamation 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;

    }

  2. #2
    Join Date
    Jan 2001
    Posts
    588

    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;
    }

  3. #3
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: pointer access --0xC0000005: Access Violation

    Code:
    char *ftr[MAX];
    Creates an array of pointers to character which point to nowhere. And not an array of characters.

    Code:
    char ftr[MAX];
    Is what you need.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  4. #4
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    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;
    
    }

  5. #5
    Join Date
    Jul 2005
    Posts
    2

    Re: pointer access --0xC0000005: Access Violation

    thanks guys!! appreciated.

  6. #6
    Join Date
    Jun 2002
    Location
    Moscow, Russia.
    Posts
    2,176

    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."

  7. #7
    Join Date
    Jul 2005
    Posts
    2

    Smile Re: pointer access --0xC0000005: Access Violation

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured