Pointer with Array in C | One dimensional array with Pointer | Two dimensional array with pointer

You must first complete for statement in c language | Nesting of for in c language | for loop in C before viewing this Lesson

Pointer with Array in C | One dimensional array with Pointer | Two dimensional array with pointer

Pointers are associated closely with arrays because arrays are implemented internally in the form of pointers.

One dimensional array with Pointer in C Language

Suppose we have a one dimensional array as

int a[4]={10,33,55,07};

We can refer to array elements as:

a[0] can be referred as *(a+0)
a[1] can be referred as *(a+1)
a[2] can be referred as *(a+2)
and so on..



Program of pointers with one dimensional array
#include<stdio.h>
int main()
{
int a[4]={15,71,18,90};

printf("\n%u",a); /*Address of a[0]*/
printf("\n%u",&a[0]); /*Address of a[0]*/

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

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

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

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

return(0);
}



Two dimensional array with pointer in C Language

Suppose that we have a two dimensional array initialized as
int a[2][2]={10,20,30,40};
We can also refer to array elements with the help of pointers as:
a[0][0] can be referred as *(*(a+0)+0)
a[0][1] can be referred as *(*(a+0)+1)
a[1][0] can be referred as *(*(a+1)+0)
a[1][1] can be referred as *(*(a+1)+1)

Program Two dimensional array with pointers.
#include<stdio.h>
int main()
{
int arr[2][2]={10,20,30,40};
printf("\n%u",arr); /*Address of arr[0][0]*/
printf("\n%u",arr[0]); /*Address of arr[0][0]*/
printf("\n%u",&arr[0][0]); /*Address of arr[0][0]*/
printf("\n%d",*(*(arr+0)+0)); /*Value of arr[0][0]*/
printf("\n%d",*(*(arr+0)+1)); /*Value of arr[0][1]*/
printf("\n%d",*(*(arr+1)+0)); /*Value of arr[1][0]*/
printf("\n%d",*(*(arr+1)+1)); /*Value of a[1][1]*/
return(0);
}





 

Spread the love
Lesson tags: One dimensional array with Pointer, 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
Spread the love