C Program to Access Array Elements Using Pointer

This program declares an array of five elements, and the elements of the array are accessed using a pointer.

To understand this example, you should have the knowledge of the following C programming topics:

Example: Access Array Elements Using Pointers

#include <stdio.h>

int main()
{
   int data[5], i;
   printf("Enter elements: ");

   for(i = 0; i < 5; ++i)
     scanf("%d", data + i);

   printf("You entered: \n");
   for(i = 0; i < 5; ++i)
      printf("%d\n", *(data + i));

   return 0;
}

Output

Enter elements: 1
2
3
5
4
You entered: 
1
2
3
5
4

In this program, the elements are stored in the integer array data.

Then, using the for loop, each element in data is traversed and print using the pointer method.

Visit this page to learn about relationship between pointer and arrays.