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

后端 未结 4 1204
天涯浪人
天涯浪人 2020-12-04 15:25

Consider:

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

and

char *s = \"abcd\";
char *a = s; 
foo1(&a)         


        
相关标签:
4条回答
  • 2020-12-04 15:51

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

    *p++
    

    means dereference the pointer, then increment the pointer itself by 1 (as usually, according to the rules of pointer arithmetic, it's not necessarily one byte, but rather sizeof(*p) regarding the resulting address). On the other hand,

    *p += 1
    

    means increment the value pointed to by the pointer by one (and do nothing with the pointer itself).

    0 讨论(0)
  • 2020-12-04 15:57

    Precedence of prefix ++ and * is same. Associativity of both is right to left. Precedence of postfix ++ is higher than both * and prefix ++. Associativity of postfix ++ is left to right.

    0 讨论(0)
  • 2020-12-04 16:11

    Precedence. The postfix ++ binds tighter than the prefix * so it increments p. The += is at the low end of the precedence list, along with the plain assignment operator, so it adds 1 to *p.

    0 讨论(0)
  • 2020-12-04 16:16

    Let's start with *p += 1

    I will try to answer this from a bit of a different angle... Step 1 Let's look at the operators and the operands: In this case it is one operand (the pointer p), and we have two operators, in this case * for dereferencing and += 1 for increment. Step 2 which has the higher precedence * has higher precedence over +=


    *P++ This one is a bit trickier... maybe even wicked Again we have one operand (p the pointer) and two operators, only now the * for dereference and ++ post increment are of the same precedence. (In some tables the ++ in a post is a higher precedence.)

    Step 1 Let's look at the operators and the operands: In this case it is the operand, and you have two operators, in this case * for dereferencing and ++ for increment. Step 2 which has the higher precedence? ++ has higher precedence over * Note: even if they have the SAME precedence they associate right to left, again, the ++ is before * Step 3 (the tricky part...) Where is ++ ? it is to the right side of the operand, which means POST Increment In this case, the compiler take a 'mental note' to perform the increment AFTER it is done with all the other operators... What after means? It means that it will only apply the increment as the very very very last step before the next ';' so it will be done with all other operators that are on the same 'line' note: if it was *++p then it will do it BEFORE any other operator on the same line so in this case, it is as equivalent to taking two of the processor's register, one will hold the value of the dereferenced *p and the other will hold the value of the incremented p++, the reason in this case there are two, is the POST activity... This is where in this case it is tricky, and it looks like a contradiction. One would expect the ++ to take precedence over the *, which it does, only that the POST means that it will be applied only after ALL other operands, BEFORE the next ';' token...

    Like I said, the tricky part is that any increment that is to the right of an operand will be set aside, and will be applied as the LAST operation before it moves on to the next line...

    0 讨论(0)
提交回复
热议问题