Declare Array in C++

ARRAYS 1-D & 2-D

You can declare 1-D array like this which can be accessed by indexes which start from 0. Keep in mind the whole array will be created in STACK Memory.

image.png


In this type we declare pointer to Array which gets stored in STACK. But we use new keyword to create array in HEAP. It means that point is pointer to an array with 5 size in HEAP. You can access the element in normal way like point[i]. Where i ranges from 0 to 4.

image.png


This is the way how you can declare 2-D Array. Keep in mind this will be created in STACK. You can access the elements in Array like Arr[i][j] where i ranges from 0 to 3 and j ranges from 0 to 4.

image.png


In this we are declaring a 2-D Array with the help of pointer named point. We are creating a array of Pointer with size 4 in STACK. And with the help of each pointer we will create a array in HEAP of size 5. This can also be accessed like point[i][j];

image.png


In this we are creating a Double Pointer that will point to an Array of Pointer which is created in HEAP. And that Pointers will create array of 5 size in HEAP. Here both Array of Pointer and Array of Variable is in HEAP. We just have double pointer created in STACK

image.png


So This is how we can declare array in various ways in C++ Note :- If you are creating a memory in HEAP don't forget to free the memory by deleting it else it will lead to memory Leak.