'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?
Solution 1:[1]
create a pointer to an array of size n, where every entry is a pointer to integer
You'll get an array of pointers
Solution 2:[2]
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).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Roim |
| Solution 2 | Vishal A. |
