Nilesh Kumar
Nilesh Kumar

Reputation: 55

Address of a pointer

Why can't we store the address of a pointer in another pointer? Pointer is just a special type of a variable and it has some address, but why can't I store that address into another pointer?

int main()
{
    int * ptr;
    int * q;
    q = &ptr;
}

Why is this code wrong?

Upvotes: 3

Views: 99

Answers (1)

Mike Seymour
Mike Seymour

Reputation: 254421

You can store the address of a pointer - or any other object type - in another pointer. But you have to get the type right; you're trying to store the address of a pointer in a pointer to int, not a pointer to a pointer.

int ** q = &ptr;   // pointer to pointer to int

Upvotes: 12

Related Questions