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