Reverse an Array in C

The task is to reverse the elements of the given array. We can use many approaches to do this, and it is a basic problem. It is one of the most frequently asked questions in interviews. This tutorial lists all the possible approaches to reversing an array in C.

For example, Suppose given an array:

Reverse an Array in C

We need to reverse the array and print the following:

Reverse an Array in C

We can use list slicing in Python and specify the step as -1 -> list [::-1] to get the reversed list. In C, there is no slicing mechanism. Hence, we need to dive into lower-level solving.

Possible Approaches:

  1. Declaring another array
  2. Iteration and Swapping
  3. Using pointers

1. Declaring another array

We can declare another array, iterate the original array from backward and send them into the new array from the beginning.

Code:

Output:

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

Here is an example:

If the array is:

Reverse an Array in C

We create another array rev with the same size = 6:

Reverse an Array in C

First iteration: i = n - 1 = 5, i >= 0, j = 0

rev[0] = arr[5]

In the same way:

Second iteration: rev[1] = arr[4]

Third iteration: rev[2] = arr[3]

Fourth iteration: rev[3] = arr[2]

Fifth iteration: rev[4] = arr[1]

Sixth iteration: rev[5] = arr[0]

Hence, the resultant reversed array:

Reverse an Array in C

2. Iteration and swapping

Iterating half of the array:

We iterate the array till size/2, and for an element in index i, we swap it with the element at index (size - i - 1).

Here is an example:

If the array is:

Reverse an Array in C

size = 6, size/2 = 3

First iteration: i = 0, i < 3

We swap arr[i] with arr[n - i - 1]

arr[0] <-> arr[5]

Reverse an Array in C

Second iteration: i = 1, i < 3

We swap arr[i] with arr[n - i - 1]

arr[1] <-> arr[4]

Reverse an Array in C

Third iteration: i = 2, i < 3

We swap arr[i] with arr[n - i - 1]

arr[2] <-> arr[3]

Termination when i = 3 as i == n/2

The resultant reversed array:

Reverse an Array in C

Code:

Output:

Enter the size of the array: 6
Enter the elements: 1 2 3 4 5 6
The reversed array: 6 5 4 3 2 1

Iterating from two ends of the array:

We can keep iterating and swapping elements from both sides of the array till the middle element.

Code:

Output:

Enter the size of the array: 6
Enter the elements: 1 2 3 4 5 6
The Reversed array: 6 5 4 3 2 1

Here is an example:

If the array is:

Reverse an Array in C

size = 6

l = 0, h = size - 1 = 5

First iteration: l = 0, h = 5

We swap arr[l] with arr[h]

arr[0] <-> arr[5]

Reverse an Array in C

Second iteration: l = 1, h = 4

We swap arr[l] with arr[h]

arr[1] <-> arr[4]

Reverse an Array in C

Third iteration: l = 2, h = 3

We swap arr[l] with arr[h]

arr[2] <-> arr[3]

Termination when l = h = 3 as l !< h

The resultant reversed array:

Reverse an Array in C

Recursive Way:

Output:

Enter the size of the array: 6
Enter the elements: 1 2 3 4 5 6
The reversed array: 6 5 4 3 2 1

3. Using pointers

A pointer is like a special variable that can hold the addresses of other variables and pointers. We can swap the array elements using pointers rather than traditional array indexes.

Important points about pointers:

  1. A pointer has to be declared as data type* name. The pointer will be able to store the address of variables of only the specified data type.
  2. Suppose we declared a pointer *ptr; if we want the pointer to refer to variable a, we need to declare ptr = &a
  3. *ptr gives the value of the variable ptr is holding, &ptr gives the address of the pointer, ptr gives the address of the variable it is holding.
  4. When we say ptr + 1 or ptr - 1, the pointer arithmetic happens concerning the data type: If the data type is int, the size of int = 4. Hence, ptr + 1 -> ptr +1(4) will be the next address ptr will hold.

Code:

Output:

Enter the size of the array: 6
Enter the elements into the array: 1 2 3 4 5 6
The reversed array: 6 5 4 3 2 1

Understanding:

  • We declared two integer pointers, ptr1 and ptr2.
  • ptr1 points to the first element of the array, and ptr2 points to the last element of the array.
  • When we say ptr1 and ptr2 refer to the addresses of the first and last elements of the array.
  • An array stores elements in contiguous memory locations. The memory allocations for an array from the first element to the last element will be in increasing order; hence the condition ptr1 < ptr2 is used in the while loop.
  • Now, using *ptr1 and *ptr2, we swap the values at ptr1 and ptr2 with a temporary variable-temp.
  • We increment ptr1 to its succeeding element and decrement ptr2 to its preceding element using simple pointer arithmetic.
  • The swapping continues till both ptr1 and ptr2 reach the middle element or when ptr1 and ptr2 overtake the middle element of the array in their iterations.





Latest Courses