• admin@embedclogic.com

Pointer Use In Array

pointer has a very close relationship with the array. we will see it’s application with the linear and multidimensional array.

Pointer Application with One Dimensional Array

An array name represents the base address of itself.

 arr_num=Base address of Array=&arr_num[0]

Difference between arr_num and &arr_num[0]

Run the Program –

An increment in Array Pointer

Pointer takes the advantage of array’s continuous storage feature. We can access every element through a single pointer variable by stepping continuous memory locations. 

Pre-requisite: Must be aware of Use of Indirection operator.

An increment in character array pointer:

We can see every element is stored in 1-byte space because this array has char datatype and char datatype need only 1-byte storage.

— A char pointer will jump 1 byte in a single increment.See the below example –

Explanation

For i=0  :  *(ptr_name+i) = *(name+0)= *(&name[0]+0) =*(0028FF14+0)=*(0x0028FF14)=10

For i=1  :  *(ptr_name+1) = *(name+1)= *(&name[0]+1) =*(0028FF14+1)=*(0x0028FF15)=20

An increment in integer array pointer

Let us take an integer array –

unsigned int arr_num[5] = {10,20,30,40,50};

Memory assignment of above array is like this –

let us see addresses through a program-

Output:

Explanation

For i=0  :  *(ptr_arr+i) = *(arr_num+0)= *(&arr_num[0]+0) =*(0028FF04+0)=

*(0x0028FF04)=10

For i=1  :  *(ptr_arr+1) = *(arr_num+1)= *(&arr_num[0]+1) =*(0028FF04+1)=

*(0x0028FF08)=20

For i=2  :  *(ptr_arr+2) = *(arr_num+2)= *(&arr_num[0]+2) =*(0028FF04+2)=

*(0x0028FF0C)=30.

Remember:

  • char pointer jumps only 1 byte in single increment whatever the processor size is.
  • int pointer jumps only 4 byte in single increment if processor is 32 bit.
  • float pointer jumps only 4 byte in single increment if processor is 32 bit.
  • double pointer jumps only 8 byte in single increment if processor is 32 bit.

Pointer Application with Two Dimensional Array

Although a two-dimensional array has multiple rows yet its memory map is linear as the one-dimensional array.see the below diagram –

2-D Matrix/Array representation
2-D Matrix/Array Memory Diagram

So in above diagram, you can see that in memory stores same as the 1-D array so you can access every element through a single pointer as we do in the one-dimensional array.

Points To remember for two-dimensional array pointer

E.g. unsigned int arr[][3]={10,20,30,40,50,60};

arr=&arr=&arr[0]=&arr[0][0]=Base address of Array.

arr=&arr[0]= represents the base address of 1-D array block1.

&arr[0][0]= represents the address of 0th row and 0th coloumn.

&arr = represent the base address of complete Matrix.

See the below diagram to understand the physical meaning of arr,&arr,&arr[0],&arr[0][0].

Program

Program to show the addresses of arr,&arr,&arr[0],&arr[0][0] and increment behaviour.

Output:

Subscribe and stay updated with our latest articles.