Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

++ Operator on pointers

My CS course is taking a bit of a turn from Java to C. I'm busy going over pointers at the moment and I have come across that the ++ operator for incrementing doens't work when dereferencing. This is more just a curiosity question than anything else. Just not used to the pointers concept just yet. Am I just doing something wrong or is it something to do with pointers?

For example:

*pointer++; Will not increment the value.
*pointer+=1; Will increment the value.

Thanks in advance!

like image 691
Nick Corin Avatar asked Feb 26 '26 07:02

Nick Corin


2 Answers

When you want to increment the value you have to make sure you use parenthesis.

(*pointer)++;
like image 133
Scotty Bauer Avatar answered Feb 28 '26 21:02

Scotty Bauer


*pointer++;

is equivalent to

*(pointer++);  // pointer is incremented

and not to

 (*pointer)++;  // pointee is incremented
like image 34
ouah Avatar answered Feb 28 '26 19:02

ouah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!