how the expression *p++ and ++*p works differently

前端 未结 2 1211
执笔经年
执笔经年 2021-01-28 01:52
#include
int main()
{
    int a[2]={10,4};
    int *k;
    int *j; 
    j=a;
    k=j; 
    printf(\"j=%d,k=%d\\n\\n\",*j,*k);
    *j++;
    ++*k;
    prin         


        
2条回答
  •  忘掉有多难
    2021-01-28 02:15

    You need to dig out your operator precedence table.

    *p++ is evaluated as *(p++)

    ++*p is evaluated as ++(*p)

    The second one is due to the prefix ++ having the same precedence as pointer dereference * so associativity (which is from right to left for those operators) comes into play.

    For completeness' sake, *(p++) dereferences the current value of p, and p is increased by one once the statement completes. ++(*p) adds 1 to the data pointed to by p.

提交回复
热议问题