Pointers-6 Pointers & Arrays

Pointers & Arrays :
------------------------
Array elements are declared in sequential memory locations. For instance the following declaration,

int a[5]; 

declares an array with 5 integer elements and stored sequentially in the memory and logically it is shown in below diagram,
To handle array operation using pointer we need to store the memory address of first element into a pointer. To get the address of first element of array we write the following statement,

&a[0]  

or 

a /* just array name also represents the address of first element */


Now next step is to store this address into pointer. It can be done in this way,

int *p = &a[0];

or 

int *p = a;

Now pointer variable p is pointing to first element of array i.e. according above diagram it is pointing to memory location 1000. To goto next element of array just increment the pointer by 1 i.e.

p=p+1; or p++;

say to goto a 5th element of array directly we can write the following statement,

p=p+5;


Comments