0

If we have

int i = 5; /*line 1*/
int *p = &i; /*line 2*/
*p++; /*line 3*/ 

What is the order of evaluation for line 3? (I know that after this p might point to something weird - but I'm not interested in the correct memory addressing but the order of evaluation, as both * and ++ seems to have same precedence)

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Alex
  • 5,510
  • 8
  • 35
  • 54
  • No they don't have the same precedence. Postfix form ++ has higher precedence than *, unlike prefix form. – StoryTeller - Unslander Monica May 09 '17 at 11:04
  • A simple way to remember that `++` increments the pointer is to note the usual single-line strcpy implementation: `while(*dst++ = *src++);` which would fail if `++` incremented actual characters instead of pointers. – vgru May 09 '17 at 11:09
  • 3
    Operator precedence is a [discoverable thing](http://en.cppreference.com/w/c/language/operator_precedence). – Oliver Charlesworth May 09 '17 at 11:13
  • @OliverCharlesworth I used the precendence table from "C Programming Language" (K&R page 48) where * and ++ sit on the same row. – Alex May 09 '17 at 11:41

3 Answers3

4

No, postfix increment has higher precedence.

In your case, the side-effect of the increment will take place after the value evaluation note (with the dereference operator). The result of the value evaluation, though, is discarded (you did not make any effort to store the result).

So, finally, the result will be the equivalent as p++;


Note:

Quoting C11, chapter §6.5.2.4, (emphasis mine)

The result of the postfix ++ operator is the value of the operand. As a side effect, the value of the operand object is incremented (that is, the value 1 of the appropriate type is added to it). See the discussions of additive operators and compound assignment for information on constraints, types, and conversions and the effects of operations on pointers. The value computation of the result is sequenced before the side effect of updating the stored value of the operand. [...]

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • So, even if ++ (by having a higher precedence) should be evaluated first it is postponed until the *p is evaluated? – Alex May 09 '17 at 11:21
  • @Alex Yes, the "increment" is a side effect, the value of the postfix `++` is the value of the operand itself, as already highlighted in my answer. – Sourav Ghosh May 09 '17 at 11:25
  • @Alex - Indeed - evaluation order and precedence are essentially unrelated; see http://stackoverflow.com/a/5475260/129570. – Oliver Charlesworth May 09 '17 at 11:26
0

*p++ is treated as *(p++) because precedence of postfix ++ is higher than *.

msc
  • 33,420
  • 29
  • 119
  • 214
  • are you sure about this? – Sourav Ghosh May 09 '17 at 11:11
  • @SouravGhosh Yes sir. – msc May 09 '17 at 11:12
  • 1
    @SouravGhosh - Semantically it's a correct assessment. A compiler will probably sequence the load before the increment, but for the C abstraction as defined by the standard `*p++` is semantically equivalent to `*(p++)`, as **rsp** said. – StoryTeller - Unslander Monica May 09 '17 at 11:14
  • 1
    @StoryTeller and rsp - Correct, thanks for confirming. :) – Sourav Ghosh May 09 '17 at 11:18
  • Why then all string functions are done like this: `while (*str) something = *str++`. If string is `abcd` then values set to `something`needed to be `bcd` according to your answer and all the comments! `*p++`is evaluated in a ways that it is first read/write of current `p` address and then increment is executed. – unalignedmemoryaccess May 09 '17 at 12:54
-2

You have to put parenthesis around the *p for this to work:

int i = 5; /*line 1*/
int *p = &i; /*line 2*/
(*p)++; /*line 3*/ 
printf("Hello, World! %d\n", *p); 

See: http://en.cppreference.com/w/c/language/operator_precedence

EricWerk
  • 167
  • 4
  • 3
    For *what* to work? The OP didn't specify what he wishes was to happen, only asking how those three lines are evaluated. – StoryTeller - Unslander Monica May 09 '17 at 11:11
  • You're right. In fact OP should just have compiled and run a few variations on operator precedence, and see how they matched expectations. Oh, well - SO is not an easy game to win points on! :-) – EricWerk May 10 '17 at 12:36