# Relationship between Pointer and Array

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 is at 1212 and the second a is present at 1216 hence states that each integer elements takes 4 bytes.

For Example:

```#include<stdio.h>
int main(){
int *p;
int arr={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 and arr and the next arr arr and arr arr 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);
int arr={
{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,arr,arr are stored as one in the pointer variable p. 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. ##### Soham Malakar

Hello my name is Soham Malakar am currently pursuing BCA

Coding is one of my favorite hobbies.
I love to code and an enthusiast to learn different types of programming languages.
I love to get intoxicated in the world of technology and video games.
A quick learner possessing better results.

Articles: 40