Daniele S DShot92
Daniele S DShot92

Reputation: 79

Double pointer of array of int

Declaring:

int** a[3];

Can I say that 6 pointers are being declared or not?

My reasoning is that for every cell of the array I can enter it by either *a[1] or **a[1].

Is this a correct assumption of I can only say that I've declared 3 pointer to pointers to 3 integers?

Upvotes: 0

Views: 944

Answers (3)

John Bode
John Bode

Reputation: 123596

All you can say is you've declared an array of 3 things - you may set those things to point to other things which in turn point to other things, but those other things are not created as a result of this declaration.

Upvotes: 0

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727097

Can I say that 6 pointers are being declared or not?

No, this line declares an array of three pointers. Even though each pointer could be pointing to a pointer to int, initially they are not pointing to anything.

every cell of the array I can enter it by either *a[1] or **a[1]

Each element of the array is a pointer to pointer to int - there is nothing else that could be inferred from the declaration.

You could use this declaration to make a 3-D array of integers, with each dimension having a different size, or you could stuff the entire array with NULLs. Nothing in the declaration limits the number of pointers that could be held by your array of three pointers.

Upvotes: 4

Bathsheba
Bathsheba

Reputation: 234875

No, you've declared an array of 3 int** pointers with automatic storage duration, that's all. Another 3 somethings have not been spontaneously created.

For each element to have meaning, each would have to point to a pointer to an int. The following code assigns something meaningful to the first element of the array:

int main()
{
    int** a[3];
    int foo;
    int* bar = &foo;
    a[0] = &bar;
}

Finally, note that a could decay to an int*** type if passed to a function with an int*** type as a parameter.

Upvotes: 1

Related Questions