1

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 Answers 2

2

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;

Sign up to request clarification or add additional context in comments.

3 Comments

How could I make the first element point to something after I made it point to nothing?
Does that mean I have to re-initialized to a non-null pointer
either initialze them with an actual address or give this address later. ex: int num; a[0] = &num; now you can use *a[0] to change the value in num.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.