开发者

Order of operations for dereference and bracket-ref in C

开发者 https://www.devze.com 2023-01-12 15:32 出处:网络
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,

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

精彩评论

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