Basic

Switch Case

Goto

Operators

if Statement

Nested if

While Loop

For Loop

Array

2D Array

Patterns

Excersises


C Program to Reverse an Array using Pointers


Write a C program to reverse an array elements using pointers.

  • Takes input for array size n and its elements into arr[].
  • Initializes two pointers, start at the beginning and end at the end of the array.
  • Swaps elements pointed to by start and end, moving inward until they meet.
  • Prints the reversed array after in-place swapping using pointer manipulation.
Example : pgm.c
#include <stdio.h>

int main() {
    int arr[100], n, *start, *end, temp;

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

    // Input array elements
    printf("Enter %d elements:\n", n);
    for(int i = 0; i < n; i++)
        scanf("%d", &arr[i]);

    // Set start and end pointers
    start = arr;
    end = arr + n - 1;

    // Reverse the array using pointers
    while(start < end) {
        temp = *start;
        *start = *end;
        *end = temp;

        start++;
        end--;
    }

    // Output the reversed array
    printf("Reversed array:\n");
    for(int i = 0; i < n; i++)
        printf("%d ", arr[i]);

    printf("\n");

    return 0;
}

Output :

Enter the number of elements: 5
Enter 5 elements:
1 2 3 4 5
Reversed array:
5 4 3 2 1