MCQ Pointers - Question 3

I think C is also the correct option, the correct answer is given as B. Please can you explain.

Hi kunal
option c is correct
we use option b to declare array of int: int * p = new int [x]
The LHS shows the type of pointer which new operator returns. RHS shows the type of array declared.

int **p = new int * [x] shows that new returns pointer to first element of pointer array i.e. pointer to pointer to int and RHS shows that array is of type pointer to int.

Hope it helps
Mark resolved if satisfied :slight_smile:

option B is int** p = new int*[x]
option C is int* p = new int[x]

I think there is a misunderstanding in the order of options

Oh yes i interchanged the options
int **p = new int * [x] is correct

Isn’t option B similar to creating a 2d array and option C is 1d array?

option B creates the array of pointer to int, also used in creating 2d arrays

for creating 2d (n x m) array:

int **p = new int *[n]
for(int i=0;i<x;i++){
p[i] = new int *[m]
}

using only option B creates a 1D array of pointer to int