C - pointer behavior with pre and post increment

前端 未结 6 1431
日久生厌
日久生厌 2021-01-22 08:50

In am doing some experiment in C pointers and trying to understand its behaviour. The following are my assumptions for the below codes. Correct me if I am wrong. I have the foll

6条回答
  •  南笙
    南笙 (楼主)
    2021-01-22 09:12

    Expression

    *ptr++;
    

    have value

    *ptr
    

    before incrementing ptr and then ptr is increment itself. There is no sense to write

    *ptr++;
    

    because the value of expression that is *ptr before incrementing of ptr is not used. So in fact the result of these expressions (expression-statements)

    ptr++;
    

    and

    *ptr++;
    

    is the same.

    As for expressions *++ptr and ++*ptr then in this expression *++ptr at first ptr is incremented ( that is it will point to the second element of the array) and then dereferenced and its value is the value of the second element. In this expression ++*ptr at first the value of the first element of the array is returned (that is 3) and then this value is incremented and you will get 4.

提交回复
热议问题