C Program to Access Array Elements Using Pointers

Introduction

In C programming, pointers provide a powerful way to directly access and manipulate the memory locations of variables, including elements within an array. This guide will show you how to write a C program to access and display elements of an array using pointers.

Example:

  • Input: Array [1, 2, 3, 4, 5]
  • Output: Displaying each element of the array using a pointer.

Problem Statement

Create a C program that:

  • Takes an array of integers as input from the user.
  • Uses a pointer to access and display each element of the array.

Solution Steps

  1. Include the Standard Input-Output Library: Use #include <stdio.h> for standard input-output functions.
  2. Declare the Array and Pointer Variable: Declare an integer array to store the elements and a pointer variable for accessing the elements.
  3. Input the Array Elements: Use a loop to take input for the array elements from the user.
  4. Access and Display Array Elements Using Pointers: Use the pointer to traverse and display each element of the array.
  5. Display the Elements: Use printf to display the elements accessed by the pointer.

C Program to Access Array Elements Using Pointers

#include <stdio.h>

int main() {
    // Step 2: Declare the array and pointer variable
    int arr[100], n;
    int *ptr;

    // Step 3: Input the number of elements and the array elements
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    printf("Enter the elements of the array: ");
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Step 4: Initialize the pointer to point to the first element of the array
    ptr = arr;

    // Step 5: Access and display the array elements using the pointer
    printf("Array elements accessed using pointer: \n");
    for (int i = 0; i < n; i++) {
        printf("Element %d: %d\n", i + 1, *(ptr + i));
    }

    return 0;  // Return 0 to indicate successful execution
}

Explanation

Step 2: Declare the Array and Pointer Variable

  • The integer array arr is declared to store the elements of the array.
  • The pointer ptr is used to access the elements of the array.

Step 3: Input the Array Elements

  • The program prompts the user to enter the number of elements in the array and stores this value in n.
  • A for loop is used to take input for each element of the array.

Step 4: Initialize the Pointer

  • The pointer ptr is initialized to point to the first element of the array (arr).

Step 5: Access and Display the Array Elements Using the Pointer

  • The program uses a for loop to traverse the array using the pointer:
    • The loop runs from 0 to n-1, where n is the number of elements in the array.
    • Inside the loop, the expression *(ptr + i) is used to access the element at index i in the array by dereferencing the pointer.
    • The program prints each element of the array along with its position.

Return 0

  • The return 0; statement indicates that the program executed successfully.

Output Example

Example Output:

Enter the number of elements: 5
Enter the elements of the array: 1 2 3 4 5
Array elements accessed using pointer:
Element 1: 1
Element 2: 2
Element 3: 3
Element 4: 4
Element 5: 5

Conclusion

This C program demonstrates how to access and display elements of an array using pointers. It covers basic concepts such as pointer arithmetic, array traversal, and memory access, making it a useful example for beginners learning C programming.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top