Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does *p++ increment after dereferencing? [duplicate]

Tags:

c

operators

I'm not really sure what the order here is. Is it: 1) Dereference the value of pointer p after increasing it 2) Dereference the value of pointer p before increasing it

like image 992
Temuz Avatar asked Sep 09 '25 17:09

Temuz


2 Answers

There is no ordering between the increment and the dereference. However, the * operator applies to the result of p++, which is the original value of p prior to the increment.

like image 134
R.. GitHub STOP HELPING ICE Avatar answered Sep 12 '25 13:09

R.. GitHub STOP HELPING ICE


In the operators table, you can see that the suffix operator ++ have higher place than the * unary operator.

Hence, *p++ increase p (and not *p), and return the value of the address that p contained before the increment (since it's the suffix ++).

But the order is implementation-depend. It may begin by dereferencing p, and then increase it, and it may store the old value of p, increase it, and then dereference the old value.

like image 43
asaelr Avatar answered Sep 12 '25 13:09

asaelr