Jesper Evertsson
Jesper Evertsson

Reputation: 613

Why can I not use ++ on a pointed value?

I just noticed something that I don't quite understand regarding pointers and the ++ operator. Lets examine this code

int test = 0;
int* pTest = &test;

*pTest = *pTest + 1;
*pTest++;

When first writing this code without trying it out I expected the two last lines to do the same thing and test to get the value 2, but the last row increased the pointer address by one instead, which is what just

pTest++;

would do. Am I just missing something really obvious here or why is

*pTest++;

and

pTest++;

doing the exact same thing?

Upvotes: 0

Views: 67

Answers (1)

cadaniluk
cadaniluk

Reputation: 15229

*pTest++ is the same as *(pTest++).

Put some parantheses around it like this:

(*pTest)++;

Upvotes: 7

Related Questions