
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compare *ptr++, *++ptr and ++*ptr in C++
In C++, both ptr++ and ++ptr are used to increment pointers, but they behave differently in expressions. The difference lies in when the increment happens: before or after the value is used. This is essential when working with loops, arrays, or pointer.
Syntax
Following is the syntax to compare ptr++ vs ++ptr in C++:
ptr++: post-increment; ++ptr: pre-increment;
Following is the table to compare ptr++ vs ++ptr in C++
Operation | When Increment Happens | Value Used |
---|---|---|
ptr++ | After use | Original pointer value |
++ptr | Before use | Incremented pointer value |
We will explore how these operators behave using examples, printing pointer values and dereferenced data before and after incrementing.
- Basic increment difference
- Using in expression
- Loop behavior difference
- Array traversal with ptr++ vs ++ptr
Basic increment difference
The ptr++ increments the pointer after its current value and ++ptr increments the pointer first.
Example
In this example, we demonstrate arithmetic pointer. It starts with a pointer to an array to displays the value at the current pointer position while incrementing it (ptr++), resets the pointer and shows the value at the next position after incrementing the pointer (++ptr).
#include<iostream> using namespace std; int main() { int arr[] = {6,7,8,9}; int *ptr = arr; cout << "Value at ptr++: " << *ptr++ << endl; ptr = arr; // reset pointer cout << "Value at ++ptr: " << *++ptr << endl; return 0; }
Output
Following is the output to the above program:
Value at ptr++: 6 Value at ++ptr: 7
Using in expression
When used inside an expression, ptr++ returns the original address, while ++ptr returns the incremented address.
Example
In this example, we use pointer to assign array values to variables 'a' and 'b', while demonstrating how the pointer moves to the next element (ptr++) or before accessing the value (++ptr).
#include<iostream> using namespace std; int main() { int arr[] = {2,4,6,8}; int *ptr = arr; int a = *ptr++; cout<< "a = "<< a << ", ptr points to: "<< *ptr << endl; ptr = arr; int b = *++ptr; cout<< "b = "<< b << ", ptr points to: "<< *ptr << endl; return 0; }
Output
Following is the output to the above program:
a = 2, ptr points to: 4 b = 4, ptr points to: 4
Loop behavior difference
Inside loops, both work, but affect the order of operations differently. Post-increment fetches then moves, pre-increment moves then fetches.
Example
In this example, we demonstrate two pointer traversal methods (ptr++ and ++ptr) to iterate through an array and print the elements while showcasing how the pointer moves different in each case.
#include<iostream> using namespace std; int main() { int arr[] = {11,22,33,0}; int *ptr = arr; cout<< "Using ptr++: "; while (*ptr++) cout<< *(ptr - 1) << " "; cout<< endl; ptr = arr; cout<< "Using ++ptr: "; while (*++ptr) cout<< *ptr << " "; return 0; }
Output
Following is the output to the above program:
Using ptr++: 11 22 33 Using ++ptr: 22 33
Array traversal with ptr++ vs ++ptr
Both pre and post-increment can be used to traverse arrays, but depends upon whether the current or next value is needed during the operation.
Example
In this example, we use pointer to traverse and print an array in two ways: ptr++, it prints all elements sequentially. ++ptr, it skips the first element and prints the next two elements.
#include<iostream> using namespace std; int main() { int arr[] = {9,18,27}; int *ptr = arr; cout<<"Traversal with ptr++: "; for (int i = 0; i< 3; i++) cout<< *ptr++<< " "; cout<< endl; ptr = arr; cout<<"Traversal with ++ptr: "; cout<<*++ptr<< " "; cout<<*++ptr<< " "; return 0; }
Output
Following is the output to the above program:
Traversal with ptr++: 9 18 27 Traversal with ++ptr: 18 27