Program to reverse an array using pointers
Last Updated :
13 Jan, 2023
Prerequisite : Pointers in C/C++ Given an array, write a program to reverse it using pointers . In this program we make use of * operator . The * (asterisk) operator denotes the value of variable . The * operator at the time of declaration denotes that this is a pointer, otherwise it denotes the value of the memory location pointed by the pointer .
- reverse function : is used to reverse the array through pointers
- swap function : is used to swap two memory contents
- print function : will print the array
Approach : In reverse function we take two pointers one pointing at the beginning of the array, other pointing at end of the array. The contents of the memory location pointed by these two pointers are swapped and then the value of first pointer is increased and that of second pointer is decreased . Examples:
Input : array = 2, 4, -6, 5, 8, -1
Output : reverse_array = -1, 8, 5, -6, 4, 2
Input : array = 1, 4, -6, 8, -10, -12
Output : reverse_array = -12, -10, 8, -6, 4, 1
Implementation:
C++
// CPP program to reverse array
// using pointers
#include <iostream>
using namespace std;
// Function to swap two memory contents
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Function to reverse the array through pointers
void reverse(int array[], int array_size)
{
// pointer1 pointing at the beginning of the array
int *pointer1 = array,
// pointer2 pointing at end of the array
*pointer2 = array + array_size - 1;
while (pointer1 < pointer2) {
swap(pointer1, pointer2);
pointer1++;
pointer2--;
}
}
// Function to print the array
void print(int* array, int array_size)
{
// Length pointing at end of the array
int *length = array + array_size,
// Position pointing to the beginning of the array
*position = array;
cout << "Array = ";
for (position = array; position < length; position++)
cout << *position << " ";
}
// Driver function
int main()
{
// Array to hold the values
int array[] = { 2, 4, -6, 5, 8, -1 };
cout << "Original ";
print(array, 6);
cout << "Reverse ";
reverse(array, 6);
print(array, 6);
return 0;
}
C
// C program to reverse array
// using pointers
#include <stdio.h>
// Function to swap two memory contents
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// Function to reverse the array through pointers
void reverse(int array[], int array_size)
{
// pointer1 pointing at the beginning of the array
int *pointer1 = array,
// pointer2 pointing at end of the array
*pointer2 = array + array_size - 1;
while (pointer1 < pointer2) {
swap(pointer1, pointer2);
pointer1++;
pointer2--;
}
}
// Function to print the array
void print(int* array, int array_size)
{
// Length pointing at end of the array
int *length = array + array_size,
// Position pointing to the beginning of the array
*position = array;
printf("Array = ");
for (position = array; position < length; position++)
printf("%d ", *position);
}
// Driver function
int main()
{
// Array to hold the values
int array[] = { 2, 4, -6, 5, 8, -1 };
printf("Original ");
print(array, 6);
printf("Reverse ");
reverse(array, 6);
print(array, 6);
return 0;
}
//code contributed by dhanshriborse
OutputOriginal Array = 2 4 -6 5 8 -1 Reverse Array = -1 8 5 -6 4 2
Time Complexity: O(N), where N represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
Similar Reads
Program to Reverse a String using Pointers Given a string, the task is to reverse this String using pointers. Examples:Input: GeeksOutput: skeeGInput: GeeksForGeeksOutput: skeeGroFskeeGApproach: This method involves taking two pointers, one that points at the start of the string and the other at the end of the string. The characters are then
2 min read
Reverse an array using Stack Given an array arr[] of size n, the task is to reverse the array using Stack.Examples:Input: arr = [10, 20, 30, 40, 50] Output: 50 40 30 20 10 Explanation: Upon reversing the array becomes [50, 40, 30, 20, 10]. Therefore, the output is 50 40 30 20 10.Input: arr = [ 1 ]Output: 1Explanation: Reversing
4 min read
Program to reverse columns in given 2D Array (Matrix) Given a 2D array arr[][]of integers of size M x N, where N is the number of columns and M is the number of rows in the array. The task is to reverse every column of the given 2D array Input: arr[][] = {{3, 2, 1} {4, 5, 6}, {9, 8, 7}} Output: 9 8 7 4 5 6 3 2 1 Input: arr[][] = {{7, 9}, {1, 5}, {4, 6}
7 min read
Pointer to an Array | Array Pointer A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements.Example:C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr;
5 min read
Right rotate given Array K times using Pointers Given an array arr[] of size N and an integer K, the task is to right rotate the array K times. Examples: Input: arr[] = {1, 3, 5, 7, 9}, K = 2Output: 7 9 1 3 5Explanation: After 1st rotation - {9, 1, 3, 5, 7}After 2nd rotation - {7, 9, 1, 3, 5} Input: {1, 2, 3, 4, 5, 6}, K = 2Output: 5 6 1 2 3 4 Ap
6 min read