Tricky pointer question

后端 未结 4 938
北荒
北荒 2021-02-07 15:17

I\'m having trouble with a past exam question on pointers in c which I found from this link, http://www.cl.cam.ac.uk/teaching/exams/pastpapers/y2007p3q4.pdf

The question

4条回答
  •  無奈伤痛
    2021-02-07 15:35

    The [] operator takes precedence over the & operator. So the code is dereferencing pps to get to the first element of an array of short*. Since this element is also a pointer, we may treat it as an array and look up the element one position to the right of what it points to, wth [1]. Finally, we take the address of that element.

    It might be useful to note that &p[i] is the same as p + i - it gives you a pointer to the element i positions to the right of where p points to.

    The intermediate values are:

    pps == 0x1c
    *pps == 0x18
    &(*pps)[1] == *pps + 1 == 0x1A
    

    (the +1 adds two bytes, since it is used on a short*)

提交回复
热议问题