
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Difference between ++*p, *p++ and *++p in c++
In this section we will see what are the differences between *ptr++, *++ptr and ++*ptr in C++.
Here we will see the precedence of postfix++ and prefix++ in C or C++. The precedence of prefix ++ or -- has higher priority than dereference operator ‘*’ and postfix ++ or -- has priority higher than both prefix ++ and dereference operator ‘*’.
When ptr is a pointer, then *ptr++ indicates *(ptr++) and ++*prt refers ++(*ptr)
Example Code
#include<iostream> using namespace std; int main() { char arr[] = "Hello World"; char *ptr = arr; ++*ptr; cout << *ptr; return 0; }
Output
I
So here at first ptr is pointing ‘H’. after using ++*ptr it increases H by 1, and now the value is ‘I’.
Example Code (C++)
#include<iostream> using namespace std; int main() { char arr[] = "Hello World"; char *ptr = arr; *ptr++; cout << *ptr; return 0; }
Output
e
So here at first ptr is pointing ‘H’. after using *ptr++ it increases the pointer, so ptr will point to the next element. so the result is ‘e’.
Example Code (C++)
#include<iostream> using namespace std; int main() { char arr[] = "Hello World"; char *ptr = arr; *++ptr; cout << *ptr; return 0; }
Output
e
In this example also we are increasing the ptr using ++, where the precedence of pre-increment ++ is higher, then it increases the pointer first, then taking the value using *. so it is printing ‘e’.
- Related Articles
- Difference between ++*p, *p++ and *++p in C
- Difference between const char* p, char * const p, and const char * const p in C
- Difference between %p and %x in C/C++
- Difference between P-Type and N-Type Semiconductor
- Difference between P-N Junction Diode and Zener Diode
- Simplify:$(2x + p - c)^2 - (2x - p + c)^2$
- Subtract \( 4 p^{2} q-3 p q+5 p q^{2}-8 p+7 q-10 \) from \( 18-3 p-11 q+5 p q-2 p q^{2}+5 p^{2} q \).
- Simplify and verify the result for $p=1$.$4 p^{3} \times 3 p^{4} \times( -p^{5})$"\n
- Given that \( \frac{4 p+9 q}{p}=\frac{5 q}{p-q} \) and \( p \) and \( q \) are both positive. The value of $\frac{p}{q}$ is
- If $p,\ q,\ r$ are in A.P., then show that $p^2( p+r),\ q^2( r+p),\ r^2( p+q)$ are also in A.P.
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times in C++
- If $ p=-10$ find the value of $p^{2}-2 p-100 $
- From an external point \( P \), tangents \( P A \) and \( P B \) are drawn to a circle with centre \( O \). At one point \( E \) on the circle tangent is drawn, which intersects \( P A \) and \( P B \) at \( C \) and \( D \) respectively. If \( P A=14 \mathrm{~cm} \), find the perimeter of \( \triangle P C D \).
- In \( \Delta P Q R \), right-angled at \( Q, P Q=3 \mathrm{~cm} \) and \( P R=6 \mathrm{~cm} \). Determine \( \angle P \) and \( \angle R \).
- Solve \( 2 p^{2} q^{2}-3 p q+4,5+7 p q-3 p^{2} q^{2} \).
