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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to Reverse a String using Pointers
- C program to sort an array using pointers
- C++ Program to compare two string using pointers
- Move all zeroes to end of array using Two-Pointers
- The length of a string using pointers
- Check if a string is palindrome in C using pointers
- Why C treats array parameters as pointers?
- Pointers in C and C++ | Set 1 (Introduction, Arithmetic and Array)
- Introduction of Smart Pointers in C++ and It's Types
- Dangling, Void , Null and Wild Pointers
- What are near, far and huge pointers?
- Output of C programs | Set 64 (Pointers)
- Applications of Pointers in C/C++
- Features and Use of Pointers in C/C++
- Pointers and References in C++
- Difference between Iterators and Pointers in C/C++ with Examples
- Chain of Pointers in C with Examples
- How many levels of pointers can we have in C/C++
- Why do we need reference variables if we have pointers
- Two Pointers Technique
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.