Incrementing pointer (ptr++) and (*ptr++)

后端 未结 5 1545
被撕碎了的回忆
被撕碎了的回忆 2021-01-07 14:33

I was revisiting pointers when I had this doubt.

int *ptr;
int arr[5] = {10,20,30,40,50};
ptr = &arr[0];

Now printf(\"Value: %d\"

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-07 14:41

    The trick to this is that whenever both are unary operators, then associativity is from right to left.

    *ptr++ --> first increment pointer to next location and then dereference the value at that location.

    Similarly *++ptr -->first increment pointer and then dereference.

    ++*ptr --> first dereference value at ptr and then increment the value.

提交回复
热议问题