Pointer to Arrays

Pointer to array and array to a pointer, both are totally different things. In this section, we are going to understand the pointer to an array. Actually, the name of the character array (string), itself is a pointer. Below lines will print the address of str string.

Ex1. 
        char str[10]=”abcdef”;
        printf(“%u”,str);  //%u is used to print address 

Ex2. 
        char str[10]=”abcdef”;
        printf(“%u”,&str[0]);  

Here the name of the string (here str) is pointing to the 0th element of the array. So Ex1 and Ex2 will show the same outputs.

Pointer to 1-D array

The syntax to write a pointer to an array is as listed below. There are two ways to write the syntax of a pointer to an array. In the 1st method, we just write the name of an array at the RHS side. And in the 2nd method, we write “&” operator with the name of an array with 0th index. Both types have the same meaning. We can use any of these.

<pointer name> = <array name>;

Or

<pointer name>= &<array name>[0];
Ex. 
    int arr[5];
    int *p;   

For the above example, we can write a pointer to array arr by writing p=arr or p=&arr[0]. Because the name of an array is the address of the 0th element of the array.

Example program to understand pointer to 1-D array

  • Here is summary of program ( Refer this after understanding example program )
 <array name>[<index>] is same as *( <array name> + <index> )

arr[5] is same as *(arr+5)

The below table we have to remember always while using a pointer to an array. In the below table notation1, notation2, notation3 represents the same thing which is listed in the last value column.

Notation 1

Notation 2

Notation 3

Value

*p

arr[0]

*arr

1

*(p+1)

arr[1]

*(arr+1)

2

*(p+2)

arr[2]

*(arr+2)

3

*(p+3)

arr[3]

*(arr+3)

4

*(p+4)

arr[4]

*(arr+4)

5

(p+i) means p points to the ith element of array arr so *(p+i) gives the value of ith element of array arr

arr[i] means it gives the ith element of array arr

*(arr+i)  gives the value of ith element of array arr

 

In the below code, arr is an array of 5 integers and p is a pointer which is a pointer to array arr. Next for loop prints array elements using all notations listed in the previous table. In print, we have used all notations (notation1, notation2, notation3 ) listed in the table. In for loop, 1st i is 0 so printf statement will print values of *(p + 0)  and arr[0] and *(arr + 0) , which is 1 in the example below. The same thing will happen for i with values 1 to 4.

#include<stdio.h>
#include<conio.h>
void main()
{
  int i,arr[5]={1,2,3,4,5};
  int *p;
  clrscr();
  p=arr;

  for(i=0;i<5;i++) {
    printf("*(p+%d)=%d, arr[%d] = %d , *(arr+ %d )=%d \n",i,*(p+i),i,arr[i],i,*(arr+i));
  }
  getch();
}

Output:

Pointer and 2-D array

If the 2-D array has 3 rows and 4 columns then it is called that this array has 3 1-D arrays and each 1-D array has 4 values. Or in other words, we can say that arr is having 3 groups of element values and each group is having 4 int data.

               int arr[3][4]={{10,11,12,13},{20,21,22,23},{30,31,32,33}};

Here {10,11,12,13} is 1st 1-D array, {20,21,22,23} is 2nd 1-D array ,{30,31,32,33} is 3rd 1-D array. Let’s understand this by an example of an array having row i, column j means that array is arr[i][j]. The below table shows different notations with its meaning.

arr

Address of/Points to 0th 1-D array

*arr

Address of/Points to 0th element of 0th 1-D array

(arr+i)

Address of/Points to ith 1-D array

*(arr+i)

Address of/Points to 0th element of ith 1-D array

*(arr+i) + j

Address of/Points to jth element of ith 1-D array

*( *(arr+i) + j )   or  arr[i][j]

Value of of jth element of ith 1-D array

Example program to understand pointer and 2-D array

The below example is showing the usage of the previous table with different notations. The 1st for loop is for 1st index of arr array (0 to 2) and 2nd for loop is showing 2nd index of array arr (0 to 3). To understand the working of the below example, compare the printf statement with the previous table. For loop will start with i=0, j=0 so it will print value of arr+0 and *(arr+0)  and  *(arr+0)+0  which is the address of the 0th 1-D array (it is 6487520, it can be different in different programs and states. At the last, for loop will print value of *( *(arr+0)+0)) which is a[0][0]  (10 in this case).

#include<stdio.h>
#include<conio.h>
void main()
{
  int i,j,arr[3][4]={{10,11,12,13},{20,21,22,23},{30,31,32,33}};
  clrscr();
  for(i=0;i<3;i++)
  {
     for(j=0;j<4;j++)
     {
       printf("arr+%d = %d , *(arr+%d)=%d ,*(arr+%d) + %d = %d ,*( *(arr+%d) + %d)=%d \n",i,arr+i,i,*(arr+i),i,j,*(arr+i)+j,i,j,*( *(arr+i)+j));
     }
  }
  getch();
}

Output:

Learning from this blog:

  1. What is pointer
  2. Pointer to 1-D array and 2-d array
  3. How to add address and value of array using pointer
  4. Program of how to use pointer to 1-D,2-D array
  5. Pointer and string
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments