0

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

2
  • F. std::array<std::array<int, 5>, 3> Commented Sep 25, 2014 at 0:12
  • G. auto p = std::array<std::array<int*, 5>, 3>{{}}; Commented Sep 25, 2014 at 1:32

5 Answers 5

1

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).

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

Comments

1

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.

Comments

0

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

Comments

0

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)

Comments

0
  • 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.

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.