C Program to Access Array Elements Using Pointer
Write a C program to access array elements using pointer.
- An integer array arr of 5 elements is declared and initialized.
- A pointer ptr is set to point to the first element of the array.
- A for loop iterates over the array using pointer arithmetic (*(ptr + i)).
- Each array element is printed by dereferencing the pointer with an offset.
Example : pgm.c
#include <stdio.h> int main() { int arr[5] = {10, 20, 30, 40, 50}; int *ptr = arr; // Pointer to the first element of the array printf("Accessing array elements using pointer:\n"); for(int i = 0; i < 5; i++) { printf("Element %d = %d\n", i, *(ptr + i)); } return 0; }
Output :
Accessing array elements using pointer:
Element 0 = 10
Element 1 = 20
Element 2 = 30
Element 3 = 40
Element 4 = 50
Element 0 = 10
Element 1 = 20
Element 2 = 30
Element 3 = 40
Element 4 = 50