3

I am using C.

Say I have a pointer to a integer called *myptr

int *myptr;

What would then be the difference between:

*myptr++

and...

*(myptr++)

Thanks Dan

Supertecnoboff
  • 6,406
  • 11
  • 57
  • 98
  • 2
    first one you are incrementing the the value the pointer myprt++ points to , the other you are getting the value of the next address that myprt points to – HSN Apr 28 '13 at 11:07
  • 1
    I have answered almost the same question not so long ago: possible duplicate of [Why is \*p++ different from \*p += 1?](http://stackoverflow.com/questions/12221396/why-is-p-different-from-p-1) –  Apr 28 '13 at 11:07
  • have you tried it on your machine? – Abubakkar Apr 28 '13 at 11:07
  • 7
    To me these are identical. Instead (*myptr)++; would be different. – Aki Suihkonen Apr 28 '13 at 11:10
  • 1
    I must say it is not good practice to rely on [operator precedence](http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm) though. Always use parantheses. That would greatly help any people looking at your code in the future. – Anish Ramaswamy Apr 28 '13 at 11:11
  • @AnishRam Well, this is so idiomatic... Each and every libc implementation of `strlen()` has something like `while (*s++) len++;` in it. –  Apr 28 '13 at 11:12
  • @H2CO3, So are you saying you'd rather see `while (*s++) len++;` than `while (*(s++)) len++;`? – Anish Ramaswamy Apr 28 '13 at 11:14
  • 3
    @AnishRam Yes, exactly. –  Apr 28 '13 at 11:14
  • Operator precedence games are nice, but 3 months later when you're trying to fix a bug fast, you will regret not having parantheses. So please use them from the start! – Torp Apr 28 '13 at 11:32
  • 4
    @Torp `*pointer++` is a C idiom. You will encounter it in the code of others even if you do not write it yourself, so you have to know what it does. You can do what you want for your own code. – Pascal Cuoq Apr 28 '13 at 11:36
  • 3
    @Torp Within limits. Some precedences are so fundamental that using parentheses only confuses. Would you write `int a = b + (c*d);` or `int a = b + c*d;`? Whether `*ptr++` is so fundamental as `+` and `*` is a question. I tend towards yes. – Daniel Fischer Apr 28 '13 at 11:37
  • @Torp `*myptr++` is a very common C idiom, indeed. Unnecessary parenthesizing decreases readability. –  Apr 28 '13 at 11:43

1 Answers1

15

Since ++ has higher precedence than *, there is no difference between the two.

  • 4
    Thank you. I was starting to get worried, seeing the wrong answer in one comment and two answers. – Pascal Cuoq Apr 28 '13 at 11:11
  • @PascalCuoq You are welcome. I wasn't sure if I just misread the question, and I even tried it with a string and a char pointer before posting, but apparently I was right. –  Apr 28 '13 at 11:11
  • @H2CO3 Mate,if there is no difference between the two then how would you explain the output of the program I have posted in my answer? – Rüppell's Vulture Apr 28 '13 at 11:26
  • @Daij-Djan Are your 'practical tests' similar to those of Sheer Fish (i.e. http://ideone.com/2l51A9 )? – Pascal Cuoq Apr 28 '13 at 11:37
  • @SheerFish [Here's a correct sample code snippet.](http://ideone.com/KcVdrc) You can see that these are identical. –  Apr 28 '13 at 11:41
  • 1
    @H2CO3 PascalCuoq already explained the whole thing to me.I saw where I had gone wrong in the code.That's why I deleted my answer. – Rüppell's Vulture Apr 28 '13 at 11:48
  • @H2CO3 He used an integer array to illustrate his point though.Here are his and mine programs: http://ideone.com/HI31dJ , http://ideone.com/2l51A9 – Rüppell's Vulture Apr 28 '13 at 11:49
  • @SheerFish the code in your link increments the same variable (myptr) twice in the program hence your getting different results. the result of a postfix++ expression is a temporary rvalue object. – Koushik Shetty Apr 29 '13 at 16:45