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

前端 未结 5 1993
無奈伤痛
無奈伤痛 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:18

    It's not a function, it simply takes advantage of a little-known C fact that array indexes can be interchanged. All the x[y] notation really means is that you're accessing the xth offset of the y array. But you could just as easily write y[x] in your case and get the same result.

    99[array] and array[99] are interchangeable and mean the same thing. By declaring GetHundredthElement to be 99, your friend played a neat trick :)

    You CAN however write a generic function to get the hundredth element of an array fairly easily using C++ templates (not C).

提交回复
热议问题