1
votes

Hello stackover flow community, I'm trying initialize two empty array of pointers but getting this error on Visual Studio 2013:

Unhandled exception at 0x011C5E9C in 45CProject.exe: 0xC0000005: Access violation writing location 0x00000000.

Here is my code:

    #include <iostream>

using namespace std;

int main(){

    int* a[10] = { nullptr };
    int* b[10] = { nullptr };

    *a[0] = 2;
    *b[0] = 4;

    cout << "a[0] = " << *a[0] << endl;
    cout << "b[0] = " << *b[0] << endl;

    return 0;
}

Much Appreciated!

2

2 Answers

2
votes

Yyou have an array of pointers. *a[0] means "data that first element(is a pointer) of the array points to. They don't point to anywhere since you initialized them with nullptr. This causes your access violation error;

0
votes

You initialized a[0] to nullptr (0x00000000), then *a[0] = 2; attempts to write on this position, which results in an access violation.