This doesn't look like a function. What is this?

前端 未结 5 1974
無奈伤痛
無奈伤痛 2021-01-06 05:02

A friend asked me to write a function in C to return the 100th element of an array. I\'m not very familiar with C, so I wasn\'t sure how to make a generic function that coul

5条回答
  •  醉梦人生
    2021-01-06 05:08

    In C, the bracket notation is short hand for pointer arithmetic. A normal use like x[i] is syntactically equivalent to *(x + i) (remember that arrays are pointers). If instead you had used i[x] then it's the same as *(i + x) and produces the same output. As noted by other answers, this approach is not a function, just an interesting technique.

提交回复
热议问题