Reverse an Array using Pointers
Here, you will get the source code of C++ program to reverse an array using pointers in C++ programming.
A pointer in an array is a variable that holds the address of an element in an array. This allows the programmer to access and manipulate the contents of the array. Pointers can also be used for dynamic memory allocation, allowing an array to grow or shrink in size.
Example of C++ Program to Reverse an Array using Pointers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include <iostream> using namespace std; main() { int arr[50],i,*p,num; cout<<"How many elements you want : "; cin>>num; cout<<"Enter the values of Array elements : "; for(i=0;i<num;i++,p++) cin>>*p; p--; cout<<"\nArray Elements in reverse order :\n"; for(i=0;i<num;i++,p--) cout<<*p<<" "; } |
Output
How many elements you want : 5
Enter the values of Array elements : 3
5
6
7
8
Array Elements in reverse order :
8 7 6 5 3
Check out our other C++ programming Examples