Pointer with Array in C

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

Pointer with Array in C

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

One dimensional array with Pointer

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
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]*/


Two dimensional array with pointer

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.
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]*/


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