Same Error what am I doing wrong? (first time with pointers)

I keep getting the error Access violation reading location 0xCCCCCCCC
I think its pointer related but as far as I can tell I created my pointers correctly its just throwing the error when trying to display

Code here:
#include <iostream>
#include <stdio.h>
using namespace std;

float foo(int a, char b){

return 0.0;
}
int main() {
//part A
char var1 = 'a', var2 = 'b', var3 = 'c', var4 = 'd';
char *p1 = &var1;
char *p2 = p1;
char* pRay[3] = {&var2, &var3, &var4};
char varRay[30]= {};
for (int j =0; j < 30; j++) {
varRay[j]= 'y'; }
char* p3 = &varRay[30];
int x = 2;
const int C= 4;
const int* P4 = &x;
const int* P5 = &C;
float (*p6)(int, char) = &foo;

// part B
cout << "Address of var1 = "<< &var1 << endl;
cout << "Contents of p1 = "<< p1 << endl;
cout << "Address of p1 = "<< &p1 << endl;
cout << "Contents of p2 = "<< p2 << endl;
cout << "Address of var2 = "<< &var2 << endl;
cout << "Address of var3 = "<< &var3 << endl;
cout << "Address of var4 = "<< &var4 << endl;
cout << "Contents of p array [1] = "<< pRay[1] << endl;
cout << "Contents of p array [2] = "<< pRay[2] << endl;
cout << "Contents of p array [3] = "<< pRay[3] << endl;
cout << "Address of char array[1] = "<< &varRay[1] << endl;
cout << "Address of char array[2] = "<< &varRay[2] << endl;
cout << "Address of char array[3] = "<< &varRay[3] << endl;
cout << "Contents of p3 = "<< p3 << endl;
cout << "Address of x = "<< &x << endl;
cout << "Contents of p4 = "<< P4 << endl;
cout << "Address of C = "<< &C << endl;
cout << "Contents of p5 = "<< P5 << endl;
cout << "Address of funcrion foo = "<< &foo << endl;
cout << "Contents of p6 = "<< p6 << endl;
system("pause");

cout << endl << endl;

// part C
cout << "Contents of var1 = "<< var1 << endl;
cout << "What p1 points to = "<< *p1 << endl;
cout << "Contents of p1 = "<< p1 << endl;
cout << "What p2 points to = "<< *p2 << endl;
cout << "Contents of var2 = "<< var2 << endl;
cout << "Contents of var3 = "<< var3 << endl;
cout << "Contents of var4 = "<< var4 << endl;
cout << "What p array [1] points to = "<< *pRay[1] << endl;
cout << "What p array [2] points to = "<< *pRay[2] << endl;
cout << "What p array [3] points to = "<< *pRay[3] << endl;
cout << "Contents of char array = ";
for (int i =0; i < 30; i++) {
cout << varRay[i]; }
cout << endl;
cout << "What p3 points to = "<< *p3 << endl;
cout << "Contents of p1 = "<< x << endl;
cout << "What p4 points to = "<< *P4 << endl;
cout << "Contents of p1 = "<< C << endl;
cout << "What p5 points to = "<< *P5 << endl;
cout << "Contents of funcrion foo = "<< foo(3, '5') << endl;
cout << "What p6 points to = "<< *p6 << endl;

system("pause");
return 0;
}
From a quick inspection:

1
2
3
4
5
6
7
    char varRay[30] = {}; // An array of 30 char
    // ...
    char* p3 = &varRay[30]; // A pointer to the 31st element of an array with 30 elements.
    // ...

    // dereferencing a pointer to a non-existent object (iow, undefined behavior:)
    cout << "What p3 points to = " << *p3 << endl;


Also:

1
2
3
4
5
6
    cout << "Contents of p1 = " << p1 << endl;
    cout << "Contents of p2 = " << p2 << endl;
   cout << "Contents of p array [1] = " << pRay[1] << endl;
    cout << "Contents of p array [2] = " << pRay[2] << endl;
    cout << "Contents of p array [3] = " << pRay[3] << endl;
    cout << "Contents of p3 = " << p3 << endl;


All result in undefined behavior. Pointers to char are treated as the address of C-style NUL-terminated strings, and you have pointers to a single char (when what you're pointing to is a valid memory address.) Casting them to void* would be recommended if you're trying to print the address the pointers hold.


Last edited on
Yes the problem is in the line
char* p3 = &varRay[30];

you are trying to access 31st element of the array which you have not allocated hence the memory access error .

Thumb rule :-if array size is N, you should access upto N-1
Good practice :-always use STL items like std::vector than array. You will get rid of such issues.
Topic archived. No new replies allowed.