Program to reverse an array using pointers
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
// 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; } |
Output:
reverse array = -1 8 5 -6 4 2
Recommended Posts:
- Program to Reverse a String using Pointers
- C program to sort an array using pointers
- Write a program to reverse an array or string
- C++ Program to compare two string using pointers
- Why C treats array parameters as pointers?
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Sort an array where a subarray of a sorted array is in reverse order
- First string from the given array whose reverse is also present in the same array
- Reverse an array upto a given position
- Reverse an array in groups of given size
- Sorting array with reverse around middle
- Reverse an array in groups of given size | Set 2 (Variations of Set 1 )
- Reverse an array without using subtract sign ‘-‘ anywhere in the code
- Features and Use of Pointers in C/C++
- Applications of Pointers in C/C++
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



