pointer has a very close relationship with the array. we will see it’s application with the linear and multidimensional array.
An array name represents the base address of itself.
arr_num=Base address of Array=&arr_num[0]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <stdio.h> int main() { int arr_num[]={10,20,30,40,50}; if(arr_num==&arr_num[0]) { printf("arr_num and &arr_num[0] both have same meaning"); } else { printf("arr_num and &arr_num[0] both are different"); } return 0; } |
Run the Program –
1 |
arr_num and &arr_num[0] both have same meaning |
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.
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<stdio.h> int main() { char name[]={'A','m','i','t'}; char *ptr_name; int i; ptr_name=name; /*as name=&name[0]*/ for(i=0;i<5;i++) { printf("Address of %dth index = %p\n",i,&name[i]); } for(i=0;i<4;i++) { printf("Value at Index %d =%c\n",i,*(ptr_name+i)); } return 0; } |
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
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-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include<stdio.h> int main() { unsigned int arr_num[]={10,20,30,40,50}; unsigned int *ptr_arr; int i; ptr_arr=arr_num; /*as arr_num=&arr_num[0]*/ for(i=0;i<5;i++) { printf("Address of %dth index = %p\n",i,&arr_num[i]); } for(i=0;i<5;i++) { printf("Value at Index %d =%u\n",i,*(ptr_arr+i)); } return 0; } |
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.
Although a two-dimensional array has multiple rows yet its memory map is linear as the one-dimensional array.see the below 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.
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 to show the addresses of arr,&arr,&arr[0],&arr[0][0] and increment behaviour.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> int main() { unsigned int arr[2][3]={{10,20,30},{40,50,60}}; printf("\nAddress of &arr is = %u\n",&arr); printf("\nAddress of (&arr+1) is = %u\n",&arr+1); printf("\nAddress of arr is = %u\n",arr); printf("\nAddress of (arr+1) is = %u\n",arr+1); printf("\nAddress of &arr[0] is = %u\n",&arr[0]); printf("\nAddress of (&arr[0]+1) is = %u\n",&arr[0]+1); printf("\nAddress of &arr[0][0] is = %u\n",&arr[0][0]); printf("\nAddress of (&arr[0][0]+1) is = %u\n",&arr[0][0]+1); return 0; } |
Output: