ro Hit
ro Hit

Reputation: 13

int** a = new int*[n](); What does this function do?

int** a = new int*[n]();

I can't find any explanation for this code, all I find is explanations for code like int *array = new int[n];. I understand it but I can't completely figure out the code above. What does it mean?

Upvotes: 1

Views: 3371

Answers (3)

Shubhangi Gavada
Shubhangi Gavada

Reputation: 1

As you (should) know, int *a = new int[n]; allocates an array of ints with size n.

So, in general, T * a = new T[n]; allocates an array of Ts with size n.

Now if you substitute T = int *, you'll get int **a = new int*[n];, which allocates an array of int *s (that is, of pointers to int).

Adding () on the right zeroes every pointer in the array (otherwise they would be uninitialized).

Upvotes: 0

HolyBlackCat
HolyBlackCat

Reputation: 96759

As you (should) know, int *a = new int[n]; allocates an array of ints with size n.

So, in general, T *a = new T[n]; allocates an array of Ts with size n.

Now if you substitute T = int *, you'll get int **a = new int*[n];, which allocates an array of int *s (that is, of pointers to int).

Adding () on the right zeroes every pointer in the array (otherwise they would be uninitialized).

Upvotes: 3

Roim
Roim

Reputation: 3066

create a pointer to an array of size n, where every entry is a pointer to integer

You'll get an array of pointers

Upvotes: 1

Related Questions