C Program to Reverse an Array

C Program to Reverse an Array

Reversing an array is a common task in C programming. It can be useful in many situations such as processing sequences, rearranging data, or applying algorithms. Learning different methods of reversal strengthens your understanding of loops, pointers, recursion, and memory management.

In this tutorial, we will explore five approaches to reverse an array:

  • Using a loop (in-place)
  • Using a new array
  • Using recursion
  • Using pointers
  • Using memcpy

Reversing an Array Using a Loop (In-Place)

The most direct method is swapping elements from the beginning and end, moving toward the center. This method changes the original array without using extra memory.

#include <stdio.h>

int main() {

    int n, i;

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

    int arr[n];

    printf("Enter %d elements: ", n);

    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Original array: ");

    for(i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    for(i = 0; i < n / 2; i++) {

        int temp = arr[i];
        arr[i] = arr[n - i - 1];
        arr[n - i - 1] = temp;

    }

    printf("\nReversed array: ");

    for(i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    return 0;

}

Here, elements are swapped pair by pair from start and end until the middle is reached. This method is efficient and does not require extra memory.

Reversing an Array by Creating a New Array

If you want to preserve the original array, you can copy the elements into a new array in reverse order.

#include <stdio.h>

int main() {

    int n, i;

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

    int original[n], reversed[n];

    printf("Enter %d elements: ", n);

    for(i = 0; i < n; i++) {
        scanf("%d", &original[i]);
    }

    for(i = 0; i < n; i++) {
        reversed[i] = original[n - i - 1];
    }

    printf("Original array: ");

    for(i = 0; i < n; i++) {
        printf("%d ", original[i]);
    }

    printf("\nReversed array: ");

    for(i = 0; i < n; i++) {
        printf("%d ", reversed[i]);
    }

    printf("\n");

    return 0;

}

Here, reversed[i] = original[n - i - 1] ensures each element from the end of original is placed at the beginning of reversed. This method preserves the original array while producing a new reversed copy.

Reversing an Array Using Recursion

Recursion can reverse an array by swapping the outer elements and then calling the function on the smaller sub-array.

#include <stdio.h>

void reverseArray(int arr[], int start, int end) {

    if (start >= end)
        return;

    int temp = arr[start];
    arr[start] = arr[end];
    arr[end] = temp;

    reverseArray(arr, start + 1, end - 1);

}

int main() {

    int n, i;

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

    int arr[n];

    printf("Enter %d elements: ", n);

    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Original array: ");

    for(i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    reverseArray(arr, 0, n - 1);

    printf("\nReversed array: ");

    for(i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    return 0;

}

Here, the function swaps the first and last elements, then calls itself for the remaining middle section. Recursion makes the logic simple, but it uses more function calls than loops.

Reversing an Array Using Pointers

Pointers allow us to directly access and manipulate array elements through their memory addresses.

#include <stdio.h>

void reverseWithPointers(int *arr, int n) {

    int *start = arr;
    int *end = arr + n - 1;

    while(start < end) {

        int temp = *start;
        *start = *end;
        *end = temp;
        start++;
        end--;

    }

}

int main() {

    int n, i;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    int arr[n];

    printf("Enter %d elements: ", n);

    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    printf("Original array: ");

    for(i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    reverseWithPointers(arr, n);

    printf("\nReversed array: ");

    for(i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    return 0;

}

Here, two pointers are moved inward from the start and end of the array, swapping elements until they meet. This method is efficient and shows how pointer arithmetic works in C.

Reversing an Array Using memcpy

The standard library function memcpy can be used to copy elements from a temporary reversed array back into the original array.

#include <stdio.h>
#include <string.h>

int main() {

    int n, i;

    printf("Enter number of elements: ");
    scanf("%d", &n);

    int arr[n], reversed[n];

    printf("Enter %d elements: ", n);

    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    for(i = 0; i < n; i++) {
        reversed[i] = arr[n - 1 - i];
    }


    printf("Original array: ");

    for(i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }


    memcpy(arr, reversed, n * sizeof(int));


    printf("\nReversed array: ");

    for(i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    printf("\n");

    return 0;

}

Here, elements are first placed into a temporary array in reverse order, then copied back into the original array using memcpy. This demonstrates how C’s library functions can simplify array operations.

FAQs

Q1: Can this method work for arrays of floats or doubles?
Yes, the same logic applies. You only need to change the array type and adjust sizeof() when using memory functions.

Q2: What is the difference between in-place reversal and creating a new array?
In-place reversal modifies the original array, saving memory. Creating a new array preserves the original data but uses extra space.

Q3: Can I reverse a multidimensional array?
Yes, but you need nested loops to reverse each dimension or row carefully.

Conclusion

Reversing arrays is a fundamental skill in C programming. The in-place loop method is the most efficient starting point for beginners, as it builds a clear understanding of array indexing and element swapping. Creating a new array is useful when you need to preserve original data, while recursion offers an elegant way to think about problems step by step. Using pointers helps develop a deeper grasp of memory and addresses, and memcpy shows how standard library functions can simplify operations. Together, these techniques provide a solid foundation for tackling more advanced array and algorithm challenges.

References & Additional Resources

  1. Kernighan, Brian W., and Dennis M. Ritchie. The C Programming Language. 2nd Edition, Prentice Hall, 1988.
  2. TutorialsPoint: Arrays in C – Comprehensive array tutorials.
  3. Programiz: C Programming Examples – Beginner-friendly examples.
  4. Stack Overflow: Reversing an Array in C – Community discussion and solutions.
Scroll to Top