The technique of pointer discussed in the previous article can also be utilized in the case of arrays. Where the address of the variables in an array can be used to call them. Let us see some examples.
To be more specific let’s revise what is a pointer. A pointer is a variable which holds the address of a variable, and helps in direct memory accessibility.
Let us take an examples of pointer operation in a 1D array:
In an 1D array if the pointer is initialized to the array, the pointer by default stores the address of the first variable and acts for the specific operation.

In this case, each integer variable is of 4 bytes hence the array variable addresses are initialized on that basis. The first element a[0] is at 1212 and the second a[1] is present at 1216 hence states that each integer elements takes 4 bytes.
For Example:
#include<stdio.h> int main(){ int *p; int arr[5]={4,5,6,7,8},I; p=arr; //array initialized to pointer for(i=0;i<5;i++) { printf("\nThe value of %d is %d",(i+1),*(p+i)); printf("\nThe address of %d is %d",(i+1),(p+i)); } return 0; }
The above program shows the technique of using pointer in 1D array, the pointer variable p is used to print the values of the array.
This is possible because the pointer holds the address of the first array variable and keeps on incrementing as the value of i is incremented.
The next print statement prints the address value, the only difference is the * symbol which is used when the value of the variable is displayed or required for operation.
Output:
Let us take an example of pointer operation in 2D array:
In 2D array the pointer works the same as that of 1D array. Where the first row and column variables are stored in a single address, the second row and column in the next address of the pointer variable. This is how the pointer works in the case of a 2D array.
The values of the first row and column arr[0][0] and arr[0][1] and the next arr[1][0] arr[1][1] and arr[2][0] arr[2][1] in arr,arr+1, arr+2 and so on. This is how the addresses are stored in the pointer variable.

For Example:
#include<stdio.h> int main(){ int (*p)[3]; int arr[3][3]={ {4,5,6}, {7,8,9}, {10,11,12} }; int i,j; p=arr; //array initialized to pointer for(i=0;i<3;i++) { printf("\nThe address of %d is %d",(i+1),(p+i)); for(j=0;j<3;j++) printf("\nThe value of arr[%d][%d]=%d",(i+1),(j+1),*(*(p+i)+j)); printf("\n"); } return 0; }
In the above program, it shows how pointers are used in 2D arrays where the address of arr[0][0],arr[0][1],arr[0][2] are stored as one in the pointer variable p[0]. The addresses of the next variables are stored following the same technique discussed above.
Output:
Hence, we have reached the end of the topic where we discussed pointers in arrays, especially in 1D and 2D arrays. Hope we have cleared your doubts.
Please comment and share this article if you find anything is helpful to you and also WhatsApp me.