C meaning of (++*ptr) pointer
In the previous topic we have studied Precedence of Value at and Address Operator of Pointer. In this chapter we will be looking more advance form of pointer. We are looking one step forward to learn how (++*ptr) works ?
Meaning of (++*ptr) in C Programming :
Consider the Following Example :
int num = 20 , *ptr ; ptr = # printf("%d",++*ptr);
Explanation :
- ‘++’ and ‘*’ both have Equal Precedence
- Associativity is from Right to Left ( Expression evaluated from R->L)
- Both are Unary (Operates on single operand )
- So ‘ * ‘ is Performed First then ‘ ++ ‘
Re-Commanded Reference Article : Operator Precedence Table
Visual Understanding :
Calculation of Answer :
++*ptr = ++ ( *ptr ) = ++ ( *3058 ) = ++ ( 20 ) = 21
- Suppose ptr variable stores address 3058.
- * Operator will de-reference pointer to get value stored at that address. (i.e 20)
- Pre-increment on 20 will perform increment first then assignment
Live Example : Meaning of ++*ptr Pointer
#include<stdio.h> int main() { int n = 20 , *ptr ; ptr = &n; printf("%d",++*ptr); return(0); }
Output :
21
In the next chapter we will be learning the meaning of (*++ptr) Pointer.