### Pointer and Arrays | Using Pointers with Arrays in C

In C Programming, there is a close relationship between pointers and arrays. In C language when an array is declared, the compiler allocates a base address and sufficient amount if storage to store all the elements of the array in contiguous memory locations. The base address is the location of the first element (index 0) of the array. The compiler also defines the array name as a constant pointer to the first element. Suppose we declare an array num as follows :
`int num = {10, 20, 30, 40, 50};`
And consider that the base address of num is 2000 and assuming that each integer requires four bytes, then the elements will be stored as follows :

The name num is defined as a constant pointer pointing to the first element, num and therefore the value of num is 2000 (the num is pointed to the address 2000), which is the location where num is stored, i.e.
`num = &num = 2000`
Now at this point if we declare ptr as an integer pointer, then we can make the pointer ptr to point to the array num by the following statement :
`ptr = num;`
The above expression is same as below statement :
`ptr = &num;`
Now we can access every value of num using the increment operator with the pointer ptr : ptr++. The relationship between num and ptr is shown as
`````` ptr   = &num  = ( 2000 )
ptr+1 = &num  = ( 2004 )
ptr+2 = &num  = ( 2008 )
ptr+3 = &num  = ( 2012 )
ptr+4 = &num  = ( 2016 )``````
Now lets see an C example program :
```#include <stdio.h>

int main() {
int num = {10, 20, 30, 40, 50};
int *ptr;
ptr = num;
int c=0;

while(c < 5) {
printf("num[%d] address (0x%x : 0x%x) | Value : %d\n", c, ptr+c, &num[c], *(ptr+c));
c++;
}

return 0;
}```
Output :

num address (0x3a547d20 : 0x3a547d20) | Value : 10
num address (0x3a547d24 : 0x3a547d24) | Value : 20
num address (0x3a547d28 : 0x3a547d28) | Value : 30
num address (0x3a547d2c : 0x3a547d2c) | Value : 40
num address (0x3a547d30 : 0x3a547d30) | Value : 50

At the above example we access the values of array num by :
`````` *(ptr)
*(ptr+1)
*(ptr+2)
*(ptr+3)
*(ptr+4)``````
It is similar as to num, num, for example :
`````` *(ptr)    =    num
*(ptr+1)  =    num
*(ptr+2)  =    num
*(ptr+3)  =    num
*(ptr+4)  =    num``````

Next Topic :