C program to reverse an array using pointers

Write a C program to input elements in an array and reverse the array using pointers. How to reverse an array using pointers in C programming. Logic to reverse an array using pointers in C.

Example

Input

Input array elements: 10 20 30 40 50 60 70 80 90 100

Output

Reversed array: 100 90 80 70 60 50 40 30 20 10

Required knowledge

Basic C programming, Array, Pointers, Pointer Arithmetic, Pointer and Array

Logic to reverse array using pointers

In previous posts we learned to reverse an array without pointer. Here we will learn to reverse array using pointers.

Below is the step by step descriptive logic to reverse array using pointers.

  1. Input size and array elements, store it in some variable say size and arr.
  2. Initialize a pointer to first element of array say * left = arr.
  3. Initialize another pointer to last element of array say * right = (arr + size - 1).
  4. To reverse array I am using two pointers one from left side of array and other from right side. We will swap each successive elements from left to right till center element.

    I have used bitwise XOR operator to swap two successive elements of array. To swap two elements perform

    *left    ^= *right;
    *right   ^= *left;
    *left    ^= *right;
  5. Increment left pointer and decrement right pointer by 1.
  6. Repeat step 4 and 5 until (left < right).

Program to reverse an array using pointers

/**
 * C program to reverse an array using pointers
 */

#include <stdio.h>

#define MAX_SIZE 100

/* Function declaration */
void printArr(int *arr, int size);


int main()
{
    int arr[MAX_SIZE];
    int size;
    int *left = arr;  // Pointer to arr[0]
    int *right;


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

    right = &arr[size - 1];  // Pointer to arr[size - 1]

    /*
     * Input elements in array
     */
    printf("Enter elements in array: ");
    while(left <= right)
    {
        scanf("%d", left++);
    }


    printf("\nArray before reverse: ");
    printArr(arr, size);


    // Make sure that left points to arr[0]
    left = arr;


    // Loop to reverse array
    while(left < right) 
    {
        /*
         * Swap element from left of array to right of array.
         */
        *left    ^= *right;
        *right   ^= *left;
        *left    ^= *right;

        // Increment left array pointer and decrement right array pointer
        left++;
        right--;
    }


    printf("\nArray after reverse: ");
    printArr(arr, size);


    return 0;
}



/**
 * Function to print array using pointer.
 *
 * @arr     Pointer to array.
 * @size    Size of the array.
 */
void printArr(int * arr, int size)
{
    // Pointer to arr[size - 1]
    int * arrEnd = (arr + size - 1);

    /* Loop till last array element */
    while(arr <= arrEnd)
    {
        printf("%d, ", *arr);

        // Move pointer to next array element.
        arr++;
    }
}

Output

Enter size of array: 10
Enter elements in array: 10 20 30 40 50 60 70 80 90 100

Array before reverse: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
Array after reverse: 100, 90, 80, 70, 60, 50, 40, 30, 20, 10,