Declarations of Array:

Pasted image 20231213164826.png

Accessing elements of array :

Pasted image 20231213165002.png

To print the address of the array :

Pasted image 20231213165646.png Pasted image 20231213165659.png

Static and Dynamic Array :

• Static array means the size of an array is static I.e; fixed
• Dynamic array means the size of an array is Dynamic I.e; flexible
• When an array is created it is created inside stack memory
• The size of the array is decided during at compile time
• When declaring an array it must be a static value only and not variable type in c language however in c++ dynamic allocation is possible during compile time

We can create array inside Heap
• When accessing any value inside a heap it must be done through a pointer

Pasted image 20231213172034.png

  • If we don't release the memory then it causes memory leak problems.
  • Here pointer acts as a name of the array.

Accessing an array in heap and stack :

Pasted image 20231213172637.png

Again,

  • A normal array is created inside a stack. When you want an array in heap you'll have to use a pointer and then allocate the memory inside heap.
#include<iostream>
using namespace std;
int main()
{
    int *p;
    p = new int [5];
    p[0]=12;
    p[1]=13;
    cout << p[0]; //output : 12
    cout << p[1]; //output : 13
    cout << p[2]; //output : harbage

    return 0;
}

Increasing the size of an array :

Method:

Create a new array of a bigger size and transfer elements of old array to this new array.
Pasted image 20231213174609.png

2D array :

Creating a 2D array :

Method 1 :

Pasted image 20231213180234.png

Method 2:

Pasted image 20231213181058.png
Here the array of pointer is in stack but the actual array is in heap.

Method 3:

Here we'll put everything in heap now.
Pasted image 20231213181452.png