Why is * p ++ different from * p + = 1?

Consider:

void foo1(char **p) { *p++; } void foo2(char **p) { *p += 1; } 

and

 char *s = "abcd"; char *a = s; foo1(&a); printf("%s", a); //abcd 

but if I use foo2() instead:

 char *a = s; foo2(&a); printf("%s", a); //bcd 

Can someone explain this?

+46
c
Aug 31 '12 at 19:22
source share
4 answers

The key is the priority of the += and ++ operator. ++ has a higher priority than += (in fact, assignment operators have the second lowest priority in C), so the operation

 *p++ 

means dereference the pointer, then increase the pointer by 1 (as usual, according to the rules of pointer arithmetic, this is not necessarily one byte, but rather sizeof(*p) relative to the resulting address), On the other hand,

 *p += 1 

means increasing the value indicated by the pointer one (and do nothing with the pointer itself).

+95
Aug 31 '12 at 19:25
source share

Extraordinary. Postfix ++ binds stricter than the * prefix, so it increments p . += is at the bottom of the priority list along with a simple assignment operator, so it adds 1 to *p .

+29
Aug 31 '12 at 19:25
source share

The prefix priority of ++ and * is the same. The associativity of both right to the left. The priority of postfix ++ is higher than both * and prefix ++. Postfix ++ associativity from left to right.

0
Jul 23 '15 at 18:13
source share

Let's start with *p += 1

I will try to answer this a little differently ... Step 1 Let's look at the operators and operands: in this case it is one operand (pointer p), and we have two operators: this case * for dereference and + = 1 for increment. Step 2, which has a higher priority *, has a higher priority over + =




*P++ This is a little more complicated ... maybe even evil Again we have one operand (p pointer) and two operators, only now * for dereferencing and postposition ++ have the same priority. (In some tables, ++ in the message is a higher priority.)

Step 1. Look at the operators and operands: in this case it is an operand, and you have two operators, in this case * for dereferencing and ++ for incrementing. Step 2, which has a higher priority? ++ takes precedence over * Note: even if they HAVE ANY INITIAL MANAGEMENT, they bind right to left, again ++ to * Step 3 (the hard part ...) Where is ++? it is on the right side of the operand, which means POST Increment . In this case, the compiler takes a β€œmental note” to perform the increment AFTER it is done with all other operators ... What after? This means that he will only apply the increment as the very last step before the next ';' so this will be done with all other operators that are on the same line, note: if it was * ++ p, then it will do this BEFORE any other operator on the same line, so in this case it is equivalent to taking two processor registers, one will hold the value of dereferenced * p and the other will hold the value of incremented p ++, the reason in this case is two, it is POST activity ... Here, in this case, it is difficult, and this looks like a contradiction. One would expect ++ to take precedence over *, which does only that POST means that it will only be applied after ALL other operands, BEFORE the next one :; marker...

As I said, the tricky part is that any increment to the right of the operand will be delayed and will be used as a LAST operation before it moves to the next line ...

0
May 2, '17 at 13:13
source share



All Articles