Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Precedence of postfix ++ and prefix ++ in C/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
#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
#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’.
Advertisements
