开发者

Order of operations for dereference and bracket-ref in C

If I do *ptr[x], is that equivalent to *(ptr[x]), or开发者_如何学Python (*ptr)[x]?


*(ptr[x])

See the Wikipedia operator precedence table, or, for a more detailed table, this C/C++ specific table.


In C, all postfix operators have higher precedence than prefix operators, and prefix operators have higher precedence than infix operators. So its *(ptr[x])


Using the counter-clockwise movement of analyzing and parsing that simple example

1. starting with ptr, work in counter-clockwise until you hit asterisk operator
2. asterisk, in counter-clockwise until you hit subscript operator
3. we arrive here, at subscript operator [x]

Since [] has higher precedence than the asterisk as per this table , that makes it *(ptr[x])

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜