Pointer with Array in C

Preview
You must first complete Introduction to Array in C | One dimensional array in C before viewing this Lesson

Pointer with Array in C

Arrays are directly associated with pointers as arrays are internally implemented with the help of pointers.

Compiler needs to know about the address of very first element of array. Addresses of rest of the elements are automatically calculated on the basis of first element’s address in memory.

Pointer and One dimensional array

Suppose that we have a one dimensional array initialized as

int arr[4]={1,3,5,7};

Address of very first element of array i.e. arr[0] is equivalent to simple array name arr which means that arr refers to address of very first element of array i.e. &arr[0].

We can also refer to array elements with the help of pointers as:

arr[0] can be referred as *(arr+0)
arr[1] can be referred as *(arr+1)
arr[2] can be referred as *(arr+2)
arr[3] can be referred as *(arr+3)

In *(arr+0), arr refers to the address of very first element of array, on adding zero to it there will be no change in address, the sign of asterisk before (arr+0) refers to the value pointed by the address generated due to (arr+0) i.e. value of arr[0].

Similarly, in *(arr+1), arr refers to the address of very first element of array, on adding one to it two bytes will be added to the address i.e. it shows the address of second element of array, the sign of asterisk before (arr+1) refers to the value pointed by the address generated due to (arr+1) i.e. value of arr[1].

Other elements of array can be referred in the similar way.


Program to demonstrate use of pointers with one dimensional array
#include<stdio.h>
int main()
{
int a[4]={5,7,8,9};printf(“\na=%u”,a); //&a[0] /*Address of first element of array*/
printf(“\n&a[0]=%u”,&a[0]); /*Address of first element of array*/

printf(“\n*a=%d”,*a); /*Value of a[0]*/
printf(“\n*(a+0)=%d”,*(a+0)); /*Value of a[0]*/

 

printf(“\n(a+1)=%u”,(a+1)); /*Address of a[1]*/
printf(“\n*(a+1)=%d”,*(a+1)); /*Value of a[1]*/

 

printf(“\n(a+2)=%u”,(a+2)); /*Address of a[2]*/
printf(“\n*(a+2)=%d”,*(a+2)); /*Value of a[2]*/

 

printf(“\n(a+3)=%u”,(a+3)); /*Address of a[3]*/
printf(“\n*(a+3)=%d”,*(a+3)); /*Value of a[3]*/

 

return(0);
}

Output
a=65518
&a[0]=65518
*a=5
*(a+0)=5
(a+1)=65514
*(a+1)=7
(a+2)=65522
*(a+2)=8
(a+3)=65524
*(a+3)=9

 

Program to demonstrate the use of pointer variable with a one dimensional array
#include<stdio.h>
int main()
{ 

int a[5]={5,7,8,9,10};
int *b;

 

b=a; //b=&a[0]

printf(“\nb=%u”,b); /*Address of first element of array*/
printf(“\n*b=%d”,*b); /*Value of first element of array*/

b++; /*Pointer jumps to second element of array*/

printf(“\nb=%u”,b); /*Address of second element of array*/
printf(“\n*b=%d”,*b); /*Value of second element of array*/

b+=2; /*Pointer jumps to fourth element of array*/
printf(“\nb=%u”,b); /*Address of fourth element of array*/
printf(“\n*b=%d”,*b); /*Value of second element of array*/

printf(“\nb=%u”,(b-1)); /*Address of third element of array*/
printf(“\n*b=%d”,*(b-1)); /*Value of third element of array*/

printf(“\nb=%u”,b); /*Address of fourth element of array*/
printf(“\n*b=%d”,*b); /*Value of fourth element of array*/

return(0);
}

Output
b=65516
*b=5
b=65518
*b=7
b=65522
*b=9
b=65514
*b=8
b=65522
*b=9




Array of Pointers

We can also take an array of pointers which means that the each array element can contain address of some other variable.The array of pointers can be declared as:
*arrname[size];
data_type represents the data type of the array to be declared.
arrname represents the name of the array of pointers. The name of array must be prefixed with the indirection operator *.
size specifies the number of elements that can be stored in the array.
Example:
int *a[3];

Program to demonstrate array of pointers.
#include<stdio.h>
int main()
{int x=10,y=14,z=30,i;int *d[3]; /*Array of pointers*/d[0]=&x; /*d[0] contains address of variable x*/
d[1]=&y; /*d[1] contains address of variable y*/
d[2]=&z; /*d[2] contains address of variable z*/printf(“\nArray elements are:=”); 

for(i=0;i<3;i++)
printf(“\n%d”,*d[i]);return(0);
}

Output
Array elements are:=
10
14
30




Pointer and two dimensional array

Just like one dimensional array, we can also work with two dimensional arrays with the help of pointers.
Suppose that we have a two dimensional array initialized as
int arr[2][2]={5,2,6,9};
Address of arr[0][0] is equivalent to simple array name arr which means that arr refers to address of very first element of array i.e. &arr[0][0].
We can also refer to array elements of two dimensional array with the help of pointers as:
arr[0][0] can be referred as *(*(arr+0)+0) OR **arr
arr[0][1] can be referred as *(*(arr+0)+1) OR *(*arr+1)
arr[1][0] can be referred as *(*(arr+1)+0) OR **(arr+1)
arr[1][1] can be referred as *(*(arr+1)+1)

Program to demonstrate use of pointers with Two dimensional array.
#include<stdio.h>
int main()
{

 

int a[2][2]={3,5,
6,8};

 

printf(“\na=%u”,a); /*Address of a[0][0]*/
printf(“\na[0]=%u”,a[0]); /*Address of a[0][0]*/
printf(“\n&a[0][0]=%u”,&a[0][0]); /*Address of a[0][0]*/

 

printf(“\na[0][0]=%d”,*(*(a+0)+0)); /*Value of a[0][0]*/
printf(“\na[0][0]=%d”,**a); /*Value of a[0][0]*/

 

printf(“\na[0][1]=%d”,*(*(a+0)+1)); /*Value of a[0][1]*/
printf(“\na[0][1]=%d”,*(*a+1)); /*Value of a[0][0]*/

 

printf(“\na[1][0]=%d”,*(*(a+1)+0)); /*Value of a[1][0]*/
printf(“\na[1][0]=%d”,**(a+1)); /*Value of a[1][0]*/

 

printf(“\na[1][1]=%d”,*(*(a+1)+1)); /*Value of a[1][1]*/

return(0);
}

Output
a=65518
a[0]=65518
&a[0][0]=65518
a[0][0]=3
a[0][0]=3
a[0][1]=5
a[0][1]=5
a[1][0]=6
a[1][0]=6
a[1][1]=8



Best Books of C





Lesson tags: one dimensional array with pointer in c, pointer array in c, pointer with array in c, two dimensional array with pointer in c
Back to: C Programming Language