0

When you have the statement:

*str++ = *end

Does the *str get assigned the value of *end or does it get incremented and then get assigned the value of *end?

SDG
  • 2,260
  • 8
  • 35
  • 77
  • 3
    What would `*(++str) = *end` do? – Iharob Al Asimi Nov 09 '15 at 13:17
  • @iharob: braces are unnecessary. – EOF Nov 09 '15 at 13:17
  • precedence of `++` is higher than the one of `*`, so `*str++` is the same than `*(str++)`. – hexasoft Nov 09 '15 at 13:18
  • Incrementing and decrementing in a bigger expression leads to difficult to find bugs. You should always use multiple lines unless pressing enter causes you physical pain. – Kijewski Nov 09 '15 at 13:21
  • @EOF they don't bother anyone and I think `*++str` is so ugly and unreadable that I would immediately stop reading the rest of the code. – Iharob Al Asimi Nov 09 '15 at 13:21
  • @JonathonReinhart I voted to reopen this because is *not* a duplicate of the linked question. The linked question is about `*p++` or `*(p++)` in general. The question above is about *when* the increment occurs relative to the assignment, which is not addressed in the linked duplicate question. There may be another question in SO that answers this, but it's not that one. – lurker Nov 09 '15 at 13:30
  • @lurker Reopened. Then this question is even less useful because it has nothing to do with pointers. It's just the classic pre-vs-post increment question that happens to use a pointer. – Jonathon Reinhart Nov 09 '15 at 13:34
  • 1
    @JonathonReinhart what I find useful about the question is the use of the post increment on the left-hand-side expression. The OP seeks clarity regarding whether the assignment operation occurs before or after *any* left-hand-side increments/decrements. The pointer is the most obvious left-hand-side use case. – lurker Nov 09 '15 at 13:35
  • 1
    @lurker Valid point. I haven't had my coffee today. But I don't drink coffee so this could be a problem! – Jonathon Reinhart Nov 09 '15 at 13:37
  • You could see this in any C book you didn't need to ask it here –  Nov 09 '15 at 13:56

2 Answers2

5

As a post-increment operator, it first assigns *end then points to new/incremented address of str.

rajuGT
  • 6,224
  • 2
  • 26
  • 44
  • Is this the only post increment operator in C? – SDG Nov 09 '15 at 13:18
  • 1
    Yes, this is the only post increment operator, not only in c, in c++, java, javascript. – rajuGT Nov 09 '15 at 13:19
  • And also its true about `--` before and after the operand –  Nov 09 '15 at 13:55
  • 1
    @SharanDuggirala it is obviously the only _post increment_ operator in C (why would there be more than one operator for the same thing) but there are other _postfix_ operators such as `[ ]`, `.`, `->`, `--`. – Lundin Nov 09 '15 at 13:57
1

Logically, the expression evaluates to something like the following:

t0 = str;
t1 = *end;
str = str + 1;
*t0 = t1;

except that the exact sequence in which these operations occur is unspecified. The following sequences are also possible:

t0 = str;
str = str + 1;
t1 = *end;
*t0 = t1;

t0 = *end;
t1 = str;
*t1 = t0;
str = str + 1;

t0 = *end;
t1 = str;
str = str + 1;
*t1 = t0;

The one constant is that we're updating the location that str points to before the increment.

John Bode
  • 119,563
  • 19
  • 122
  • 198