-2

I found this text (source: https://education.cppinstitute.org/) and I'm trying to understand the second instruction.

Can you answer the question of what distinguishes these two instructions?

 c = *p++;

and

 c = (*p)++;

We can explain: the first assignment is as if the following two disjoint instructions have been performed;

 c = *p;
 p++;

In other words, the character pointed to by p is copied to the c variable; then, p is increased and points to the next element of the array.

The second assignment is performed as follows:

 c = *p;
 string[1]++;

The p pointer is not changed and still points to the second element of the array, and only this element is increased by 1.

What I don't understand is why it is not incremented when the = operator has less priority than the ++ operator.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Martin
  • 66
  • 5
  • 1
    What? The stuff on the right will still be executed before the assignment-the explanation is telling you how they're different by providing alternative code with the same effect. – Dave Newton Sep 04 '17 at 22:11
  • I don't understand the question. The pointer won't be incremented because `++` is not applied to the pointer, but rather to the value pointed by it. – HolyBlackCat Sep 04 '17 at 22:13
  • but it says, the character pointed to by p is copied to the c variable; then, p is increased – Martin Sep 04 '17 at 22:14
  • Yes c = *p, then p++; – Omid CompSCI Sep 04 '17 at 22:14
  • 2
    It is incorrect to say that "character pointed to by p is copied to the c variable; then, p is increased" and the text does not intend to say that. In C language there's no sequencing inside `c = *p++` whatsoever, which means that there's no way to say what happens "first" and what happens "then". In that sense the above analogy with "two disjoint instructions" is misleading and invalid. It is prefectly possible that the increment will happen first. – AnT stands with Russia Sep 04 '17 at 22:16
  • The core of such a statement is `lvalue=expression;` And expression can have side effects. Assignment is a second-class citizen, C is all about expressions. – wildplasser Sep 04 '17 at 22:33
  • 1
    If you're quoting material from a site, you should (a) link to the page, not the site as a whole, and (b) quote the material using `>` signs at the start of the line. I suspect that all the material that is in italics should be quoted, and possibly the preamble part from "Can you answer…". Quoting properly shows some respect for the people who created the other site. – Jonathan Leffler Sep 04 '17 at 23:48
  • Note that the explanation for the second assignment assumes a context such as `char string[] = "Hello"; char *p = &string[0];` — the alternative execution makes no sense unless there is a loosely similar context (there are details that could be different, but the overall result must be similar). – Jonathan Leffler Sep 04 '17 at 23:50

3 Answers3

2

What I don't understand is why it is not incremented when the = operator has less priority than the ++ operator.

The value for example of the expression

x++

is the value of x before incrementing.

So if you'll write

y = x++;

then the variable y gets the value of x before its incrementing.

From the C Standard (6.5.2.4 Postfix increment and decrement operators)

2 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). ... The value computation of the result is sequenced before the side effect of updating the stored value of the operand. ...

If instead of the expression

c = (*p)++;

you'll write

c = ++(*p);

then you get the expected by you result. This demonstrates the difference between the postfix increment operator ++ and the prefix (unary) increment operator ++.

halfer
  • 19,824
  • 17
  • 99
  • 186
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
2

With respect to this statement expression

c = (*p)++;

, you say

What i dont understand is why [p] is not incremented when the = operator has less priority than the ++ operator.

There is a very simple explanation: p is not incremented as a result of evaluating that expression because it is not the operand of the ++ operator.

That is in part exactly because the = operator has lower precedence: because the precedence of = is so low, the operand of ++ is the expression (*p) rather than the expression c = (*p). Note in particular that p itself is not even plausibly in the running to be the operand in that case, unlike in the variation without parentheses.

Moving on, the expression (*p) designates the thing to which p points, just as *p all alone would do. Context suggests that at that time, that's the same thing designated by string[1]. That is what gets incremented, just as the text says, and its value prior to the increment is the result of the postfix ++ operation.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
-1

When the ++ is following a variable, the variable is incremented after it has been used.

So when you have

y = x++;

x is incremented after y gets the value of x.

This is how it works for the -- operator also.

Vaibhav
  • 484
  • 4
  • 7