Access Array Elements Using Pointers

In this example, we will declare array and access elements of array using pointer.

Access Array Elements Using Pointer In C Programming

	
#include<stdio.h>

int main()
{
    int arr[5] = {100, 200, 300, 400, 500}, i;
    int *ptr = arr;

    for(i = 0; i < 5; i++)
        printf("&arr[%d] = %p\t arr[%d] = %d\n",i,ptr+i,i,*(ptr+i));

    return 0;
}
	

Output :

	
&arr[0] = 000000000061FDF0       arr[0] = 100
&arr[1] = 000000000061FDF4       arr[1] = 200
&arr[2] = 000000000061FDF8       arr[2] = 300
&arr[3] = 000000000061FDFC       arr[3] = 400
&arr[4] = 000000000061FE00       arr[4] = 500
	

Share your thoughts

Ask anything about this examples