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 .
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
reverse array = -1 8 5 -6 4 2
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Program to Reverse a String using Pointers
- C program to sort an array using pointers
- Program to reverse the rows in a 2d Array
- Write a program to reverse an array or string
- C++ Program to compare two string using pointers
- Move all zeroes to end of array using Two-Pointers
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Why C treats array parameters as pointers?
- 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
- C program to reverse the content of the file and print it
- Program to print numbers from N to 1 in reverse order
- Sorting array with reverse around middle
- Reverse an array upto a given position
- Reverse an array in groups of given size