Open In App

Reverse Array using Pointers in C++

Last Updated : 23 Jun, 2025
Suggest changes
Share
Like Article
Like
Report

Reversing an array is a common problem in programming where the task is to reorder the elements of the array so that the first element becomes the last, the second element becomes the second last, and so on.

Example:

Input: arr[] = {10, 20, 30, 40, 50}
Output: 50 40 30 20 10

To reverse an array using pointers, we can use two pointer approach: one pointing to the start of the array and other pointing to the end of the array while swapping the values at these pointers and moving the start pointer forward and the end pointer backward until pointers meet each other.

Example Code

C++
#include <iostream> using namespace std; void reverseArray(int* arr, int size) {  // Pointer to first element  int* start = arr;    // pointer to last element  int* end = arr + size - 1;  // Until both pointers meet  while (start < end) {  // Swap values at start and end  int temp = *start;  *start = *end;  *end = temp;  // Move pointers  start++;  end--;  } } int main() {  int arr[] = {10, 20, 30, 40, 50};  int size = sizeof(arr) / sizeof(arr[0]);  reverseArray(arr, size);  for (int i = 0; i < size; i++)  cout << *(arr + i) << " ";  cout << endl;  return 0; } 

Output
50 40 30 20 10 

Similar Reads

Article Tags :
Practice Tags :