Table of contents
1.
Introduction
2.
What is Reverse An Array in C?
3.
Algorithm to Reverse an Array in C
4.
1. Reverse Array in C using a Temporary Array
4.1.
Example
4.2.
C
4.3.
Explanation:
4.4.
Complexity Analysis
5.
2. Reverse Array in C Using Iteration and Swapping
5.1.
Example
5.2.
C
5.3.
Explanation:
5.4.
Complexity Analysis
6.
3. Reverse Array in C Using Pointers
6.1.
Example
6.2.
C
6.3.
Explanation
6.4.
Complexity Analysis
7.
4. Reverse Array in C Using Function
7.1.
Example
7.2.
C
7.3.
Explanation:
7.4.
Complexity Analysis
8.
Frequently Asked Questions
8.1.
What is the most space-efficient method to reverse an array in C?
8.2.
Can these methods be used for arrays of any data type?
8.3.
How do pointers enhance the efficiency of array manipulation?
9.
Conclusion 
Last Updated: Feb 15, 2025
Easy

Reverse Array in C

Author Ravi Khorwal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Reversing an array is a basic concept in programming that involves changing the order of elements in an array so that the first element becomes the last, and the last element becomes the first. In C programming, an array is a collection of elements of the same data type, stored in contiguous memory locations. 

Reverse Array in C

What is Reverse An Array in C?

Reversing an array in C involves rearranging the elements of an array so that they appear in the opposite order.

Algorithm to Reverse an Array in C

Here is the algorithm to reverse an array in C:

1. Initialize Two Pointers:

  • Set a pointer start at the beginning of the array (0 index).
  • Set a pointer end at the last element of the array (n-1 index), where n is the size of the array.

2. Loop Until start is Less Than end:

  • Swap the elements at start and end indices.
  • Increment the start pointer (move it forward).
  • Decrement the end pointer (move it backward).

3. Repeat the Process:

  • Continue swapping elements and adjusting the pointers until start is greater than or equal to end.

4. Termination:

  • The array is now reversed, and the algorithm terminates.

1. Reverse Array in C using a Temporary Array

A more efficient way to reverse an array in C is by swapping elements in-place. This method uses two pointers: one starting from the beginning and the other from the end, swapping elements until they meet in the middle. This approach avoids extra space and improves efficiency.

Example

  • C

C

#include <stdio.h>

void reverseArray(int arr[], int size) {
int reversedArr[size];
for (int i = 0; i < size; i++) {
reversedArr[size - 1 - i] = arr[i];
}

// Optional: Copying back to original array
for (int i = 0; i < size; i++) {
arr[i] = reversedArr[i];
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);

reverseArray(arr, size);

printf("Reversed array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Reversed array: 5 4 3 2 1 


Explanation:

The function reverseArray takes an array and its size as parameters. It first declares a new array reversedArr of the same size. The for loop iterates through the array & places each element into the reversedArr from the end to the beginning. Optionally, the elements can be copied back to the original array to update it with the reversed elements directly.

Note : This method is simple & effective, but it uses additional memory for the second array, which might be an issue where we need to take care of the memory usage or wherever we need to save memory.

Complexity Analysis

  • Time Complexity: O(n)
    • The function performs two passes: one for copying to reversedArr and another for copying back (optional), both taking linear time.
  • Space Complexity: O(n)
    • Uses an additional array of the same size, increasing memory usage.

2. Reverse Array in C Using Iteration and Swapping

Another efficient way to reverse an array in C is by using the iteration and swapping approach. This method swaps the elements from the two ends of the array moving towards the center, which effectively reverses the array without the need for additional storage.

Example

  • C

C

#include <stdio.h>

void reverseArrayInPlace(int arr[], int size) {
int temp;
for (int i = 0; i < size / 2; i++) {
temp = arr[i];
arr[i] = arr[size - 1 - i];
arr[size - 1 - i] = temp;
}
}

int main() {
int arr[] = {1, 2, 3, 4, 5};
int size = sizeof(arr) / sizeof(arr[0]);

reverseArrayInPlace(arr, size);

printf("Reversed array in place: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Reversed array in place: 5 4 3 2 1 


Explanation:

The function reverseArrayInPlace works by iterating up to the middle of the array. During each iteration, it uses a temporary variable temp to swap the elements from the front & the back of the array. This approach does not require any extra space beyond the temporary variable used for swapping, making it more space-efficient than the previous method.

Note : This approach is useful when memory usage is a critical constraint, as it changes the array in place without additional memory usage.

Complexity Analysis

  • Time Complexity: O(n)
    • Each element is visited once, making the algorithm linear.
  • Space Complexity: O(1)
    • Uses only a single temporary variable (temp), avoiding extra memory usage.

3. Reverse Array in C Using Pointers

Using pointers to reverse an array in C is a powerful approach that uses direct memory access to modify the array elements. This method involves two pointers: one starting at the beginning of the array and the other at the end. These pointers move towards each other, swapping the elements until they meet in the middle.

Example

  • C

C

#include <stdio.h>

void reverseArrayWithPointers(int *start, int *end) {

   int temp;

   while (start < end) {

       temp = *start;

       *start = *end;

       *end = temp;

       start++;

       end--;

   }

}

int main() {

   int arr[] = {1, 2, 3, 4, 5};

   int *start = arr;

   int *end = arr + sizeof(arr) / sizeof(arr[0]) - 1;

   reverseArrayWithPointers(start, end);

   printf("Reversed array using pointers: ");

   for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {

       printf("%d ", arr[i]);

   }

   return 0;

}
You can also try this code with Online C Compiler
Run Code

Output

Reversed array in place: 5 4 3 2 1 


Explanation

In the reverseArrayWithPointers function, start points to the first element of the array, & end points to the last. The while loop continues as long as start is less than end. Inside the loop, elements pointed by start & end are swapped using a temporary variable temp. After each swap, start is incremented & end is decremented, effectively reversing the array in place using pointer arithmetic, which is fast & efficient.

Note : This pointer method is useful in systems programming where control over memory & performance optimization is crucial.

Complexity Analysis

  • Time Complexity: O(n)
    • The function iterates through half of the array, making it linear.
  • Space Complexity: O(1)
    • Only a temporary variable temp is used, so no extra memory is required.

4. Reverse Array in C Using Function

In this approach, we use a function to reverse the array. By defining a separate function, you can modularize the logic for reversing an array, making it reusable across different parts of your program. This is particularly useful for cleaner and more maintainable code.

Example

  • C

C

#include <stdio.h>

// Function to reverse an array
void reverseArray(int arr[], int n) {
int start = 0, end = n - 1;
while (start < end) {
// Swap arr[start] and arr[end]
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;

// Move the pointers
start++;
end--;
}
}

int main() {
// Sample array
int arr[] = {1, 2, 3, 4, 5};

// Get the size of the array
int n = sizeof(arr) / sizeof(arr[0]);

// Call the function to reverse the array
reverseArray(arr, n);

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

return 0;
}
You can also try this code with Online C Compiler
Run Code

Output

Reversed Array: 5 4 3 2 1

 

Explanation:

  • The reverseArray function swaps the elements of the array in place, starting from the first and last elements and moving towards the center.
  • The main function defines a sample array, calculates its size, and then calls reverseArray to reverse the elements.
  • The reversed array is printed as the output.

Complexity Analysis

  • Time Complexity: O(n)
    • The function iterates through half of the array, making it linear.
  • Space Complexity: O(1)
    • Uses only a single temporary variable (temp), so no extra memory is needed.

Frequently Asked Questions

What is the most space-efficient method to reverse an array in C?

The most space-efficient method is the "Iteration and Swapping" technique, as it only requires a single temporary variable for swapping and does not use additional arrays.

Can these methods be used for arrays of any data type?

Yes, these methods can be applied to arrays of any data type, including integers, characters, and structures. The key is to ensure that the swapping logic correctly handles the data size of the array elements.

How do pointers enhance the efficiency of array manipulation?

Pointers allow direct manipulation of memory locations, reducing overhead and increasing the execution speed, especially useful in low-level programming where performance is critical.

Conclusion 

Reversing an array in C is a fundamental operation that can be achieved using various approaches. The most efficient method is in-place swapping, which eliminates extra memory usage while maintaining a time complexity of O(n). For those who prefer pointer-based operations, using pointer arithmetic offers additional flexibility and performance benefits, especially in low-level programming.

Live masterclass