 # Array Representation

`int a; //Garbage valueint a={1,2,3,4,5}; //initialize and declareint a[] = {1,2,3,4,5};//iterating the arrayint a = {1,2,3,4,5};for(i=0;i<5;i++){    printf(“%d”, a[i]);}`

The array is a container that can hold a fixed number of items and these items should be of the same type. Most of the data structures make use of arrays to implement their algorithms. Following are the important terms to understand the concept of Array.

• Element − Each item stored in an array is called an element.
• Index − Each location of an element in an array has a numerical index, which is used to identify the element.

Static vs Dynamic Array
`int a; //size- compile time in c, run time in c++ (inside stack)int *p;p = (int*)malloc(5*sizeof(int)); //heap memory// If memory is not required delete it. Otherwise, it causes a memory leak.free(p); //deallocation of memory//Accessing the array inside the heapp=5;//stack array can't be resized.// Increasing the size of the array. Same array ka size nahi badhega.int *p = (int*)malloc(5*sizeof(int)); //p pointer of size 5int *q = (int*)malloc(10*sizeof(int)); //q pointer of size 10for(int i=0; i<5; i++){    q[i]=p[i]; //copy all elements from p to q}free(p); //delete p arrayp=q; //p pointer points q arrayq=NULL; //q pointer null`

2D Array
`int a;// initialize array inside stackint a = {{1,2,3,4},{5,6,7,8},{9,0,1,2}}; int *a; //inside stacka = (int*)malloc(4*sizeof(int)); //inside heap memorya = (int*)malloc(4*sizeof(int)); //inside heap memorya = (int*)malloc(4*sizeof(int)); //inside heap memoryint **a; //inside stacka = (int*)malloc(3*sizeof(int)); //inside heap memorya = (int*)malloc(4*sizeof(int)); //inside heap memorya = (int*)malloc(4*sizeof(int)); //inside heap memorya = (int*)malloc(4*sizeof(int)); //inside heap memory//access 2D arraysfor(i=0;i<3;i++){    for(j=0;j<4;j++)    {        printf("%d", a[i][j])    }}`