int **arr = new int *[100];
int *arr = new int [100];
i am confused with these two lines.what is the exact difference between them.Please explain each and every part of the line…this is so confusing.
int **arr = new int *[100];
int *arr = new int [100];
i am confused with these two lines.what is the exact difference between them.Please explain each and every part of the line…this is so confusing.
Hey @kani001
So new keyword allocates memory and return address
new int creates integer dynamically
Similarly
new int[10]
Creates 10 integers dynamically which are contiguous(one after another)
Similarly
new int*[10] creates 10 pointers f integer type contiguosly
Incase u dont know pointer is a variable which holds address
Now talking about
Here it creates 100 integers contiguosly and save its starting address in arr pointer
Similarly
Here it creates 100 integer pointers contiguosuly and save its startinng address in arr pointer
Consider LHS as int* *arr that is pointer to hold address of int* type
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
so basically in first case we create 1d array and 2nd case we create 2d array right?
Hey @kani001
In this we are creating a 1-D array of integers which will have integers value like 2,3,4,5,
Whereas
In this we are creating a 1-D array of int pointers, which will point to these variables ( like 2,3,4,5,)
Still have any issue, can ask me
in 2nd case though we create 1d array of pointers when we again point it to a linear array,then this statement (int *arr=new int[100]) should create 2d array right?)
No, it would only create a 1d array of pointers
in 2nd case though we create 1d array of pointers when we again point it to a linear array,then this statement (int *arr=new int[100]) should create 2d array right?)
in 2nd case though we create 1d array of pointers when we again point it to a linear array,then this statement (int * *arr=new int * [100]) should create 2d array right?)
Yes, if each pointer will be pointing first element of each row, then it will creat a 2d array (int * * arr = new int [n] )