Rajeshwar
Rajeshwar

Reputation: 11681

Array of int pointers

I came across this question:

In the declaration below , p is a pointer to an array of 5 int pointers.

int *(*p)[5];

which of the following statements can be used to allocate memory for the first dimension in order to make p an array of 3 arrays of 5 pointers to type int ?

A. p = new int [3][5]*;

B. p = new int (*)[3][5];

C. p = new int [3]*[5];

D. p = new int *[3][5];

E. p = new int (* [3] ) [5];

What is the answer ?

I am not sure I understand the question. Normally I would create a pointer to an array of 5 int as such int* p[5]; I am curious as to why they did it as int *(*p)[5];

Also what does the question want ? Is it asking to initialize (allocate memory) to the first 3 int pointers ? I would appreciate it if someone could explain this to me

Upvotes: 0

Views: 1175

Answers (5)

kotlomoy
kotlomoy

Reputation: 1430

  • What is the answer ?

D

  • Normally I would create a pointer to an array of 5 int as such int* p[5]; I am curious as to why they did it as int *(*p)[5];

It is not "normally" because int* p[5] is not a pointer to an array of 5 int, it is an array of 5 pointers to int.

  • Also what does the question want ? Is it asking to initialize (allocate memory) to the first 3 int pointers ?

It's not clear. There is no way "to make p an array of 3 arrays of 5 pointers to type int", to begin with.

Upvotes: 0

Anton Savin
Anton Savin

Reputation: 41341

int* p[5] has type array of size 5 of int*. It decays to int**, so p + 1 will point to the second element of that array.

int *(*p)[5] has type pointer to array of size 5 of int*. You can think of it as decayed two-dimensional array int* [][5]. So p + 1 will point to the second element of the first dimension of that array, that is to the next byte after 5 pointers to int.

Which leads us to the conclusion that the right answer is D.

(This is not to mention that other answers just don't compile regardless of type of p)

Upvotes: 0

Crowman
Crowman

Reputation: 25936

What you would write as:

int* p[5];

is a five element array of pointers to int.

What this declares:

int *(*p)[5];

is a pointer to a five element array of pointers to int, i.e. a pointer to the type of thing you just wrote.

In other words; you could do:

int * a[5];
int * (*p)[5] = &a;

You can mentally read this incrementally as follows:

(*p)               //  p is a pointer
(*p)[5]            //  p is a pointer to an array of size 5
int * (*p)[5]      //  p is a pointer to an array of size 5 of type pointer to int

You need the parentheses around *p, because otherwise:

int ** p[5];

would declare a 5 element array of type int **, or pointer to pointer to int, which is a different thing entirely.

The question is basically asking you to dynamically allocate memory equivalent to three of what a is above, so answer "D" is the correct one.

Upvotes: 1

Ahmed Fahmy
Ahmed Fahmy

Reputation: 486

The answer is
D. p = new int *[3][5];
all the others are syntactically wrong

to realize the difference between
int * p [5];
int * (*p) [5];

consider this example
int *(*p)[5];
int pp[5];
pp[0][0] = new int [5]; //LHS is int , RHS is int
,, compilation error
p[0][0] = new int [5]; //this works because p[0][0] is a pointer not an int

try thinking about each dimension as adding you additional *
back to the question
int *(*p)[5] is giving you 3 * (***p)
so you can assign
p = int *[3][5]
because it has 3 * as well

Upvotes: 0

Kerrek SB
Kerrek SB

Reputation: 477640

F:

using IPA5 = int*[5];

IPA5 * p = new IPA5[3];

Each element p[0], p[1], p[2] is just a plain, typed array of int*. There's nothing dynamic going on beyond the initial dynamic allocation, where 3 is allowed to be a dynamic quantity.

Then p[0][i] for i in [0, 5) is an int *, which you can use in whatever way you like (which includes making it point to the first element of yet anohter dynamic array).

Upvotes: 1

Related Questions