C:我如何使用 (.) 点运算符编写 ( x -> x- > x )
关于结构体和指针,如何使用点运算符编写此表达式 x->x->x
?
使用箭头运算符:x->x->x
我可以轻松访问第三个元素。使用点运算符:(*x).x
如何使用点运算符访问第三个元素?
我知道箭头运算符是点运算符的快捷方式,因此应该可以使用点运算符到达第三个元素?我可以使用一个变量:
struct node *var
var = (*ptr).next
(*var).x = some value
这真的让我很恼火。一直在教科书和互联网上到处寻找,但找不到答案。
Regarding structs and pointers, how can I write this expression x->x->x
using the dot operator?
Using arrow operator: x->x->x
I easily acces third element. Using dot operator : (*x).x
How can I acces the third element using the dot operator?
I know arrow operator is a shortcut for the dot operator, so it should be possible to reach third element using dot operator? I could use a variable:
struct node *var
var = (*ptr).next
(*var).x = some value
It really annoys me. Have been looking in text book and everywhere on internet and can't find an answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
那么
x -> x
相当于(*x).x
因此,您只需执行两次:.
与一元*
绑定得更紧密,因此优先有效。如果你感到偏执,你可以这样做:Well
x -> x
is equivalent to(*x).x
So you just do that twice:.
binds tighter that unary*
so the precedence works. If you were feeling paranoid you could do:考虑到
x->y
相当于(*x).y
,然后应用该规则两次:Considering that
x->y
is equivalent to(*x).y
, then applying that rule twice:在现实生活中您永远不会想要这样做,但是
p1
指向的对象中的成员x
;是
p2
指向的对象中的成员y
,它是p1
指向的对象中的成员,并且是成员
是p3
指向的对象中的 >zp2
指向的对象中的成员,p2 是p1
。完全有可能用更少的括号来完成,但它们肯定有助于理解。
You would never want to do this in real life, but
is the member
x
in the object pointed to byp1
;is the member
y
in the object pointed to byp2
which is a member in the object pointed to byp1
, andis the member
z
in the object pointed to byp3
, which is a member in the object pointed to byp2
, which is a member in the object pointed to byp1
.It's entirely possible that this could be done with fewer parentheses, but they definitely help with understanding.
但为什么,哦为什么???
But why, oh why????
嵌套解引用...
我认为这可以帮助您实现目标。至于你为什么要做这样的事……丑陋、丑陋、丑陋。
Nested dereferencing ...
I Think that gets you there. As to why you'd want to do such a thing ... ugly, ugly, ugly.