what is the difference between *++p, ++*p, and *p++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

what is the difference between *++p, ++*p, and *p++

20th Jan 2018, 5:29 AM
Rohan Paul
Rohan Paul - avatar
3 Answers
+ 11
The expression ++*p has two operators of same precedence, so compiler looks for assoiativity. Associativity of operators is right to left. Therefore the expression is treated as ++(*p). The expression *p++ is treated as *(p++) as the precedence of postfix ++ is higher than *. The expression *++p has two operators of same precedence, so compiler looks for assoiativity. Associativity of operators is right to left. Therefore the expression is treated as *(++p). *p++ : post increment a pointer p ++*p : pre increment the value at p location *++p : pre increment a pointer p so, *p++ is equivalent to *(p++) ++*p is equivalent to ++(*p) *++p is equivalent to *(++p)
20th Jan 2018, 5:57 AM
GAWEN STEASY
GAWEN STEASY - avatar
+ 4
*++p is evaluated as *(++p). It increments the value of p before dereferencing it. ++*p is evaluated as ++(*p). It dereferences p, and then increments it. *p++ is evaluated as *(p++). The value of p is returned to the expression before being incremented, and then p is dereferenced. http://en.cppreference.com/w/cpp/language/operator_precedence
20th Jan 2018, 5:54 AM
Hatsy Rei
Hatsy Rei - avatar
0
thanku to all of u
21st Jan 2018, 11:03 AM
Rohan Paul
Rohan Paul - avatar