简体   繁体   English

C:如何使用(。)点运算符编写(x-> x-> x)

[英]C: How can i write ( x -> x- > x ) using (.) dot operator

Regarding structs and pointers, how can I write this expression x->x->x using the dot operator? 关于结构和指针,如何使用点运算符编写此表达式x->x->x

Using arrow operator: x->x->x I easily acces third element. 使用箭头运算符: x->x->x我可以轻松访问第三个元素。 Using dot operator : (*x).x How can I acces the third element using the dot operator? 使用点运算符: (*x).x如何使用点运算符访问第三个元素?

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. 一直在教科书上和互联网上的任何地方寻找内容,找不到答案。

Well x -> x is equivalent to (*x).x So you just do that twice: 好吧x -> x等于(*x).x所以你只需两次:

(*(*x).x).x

. binds tighter that unary * so the precedence works. 一元*绑定更紧密,因此优先级起作用。 If you were feeling paranoid you could do: 如果您感到偏执,可以这样做:

(*((*x).x)).x

Considering that x->y is equivalent to (*x).y , then applying that rule twice: 考虑到x->y等于(*x).y ,然后两次应用该规则:

x->x->x;
(*x).x->x;
(*(*x).x).x;

You would never want to do this in real life, but 永远都不想在现实生活中这样做,但是

(*p1).x

is the member x in the object pointed to by p1 ; p1指向的对象中的成员x

(*((*p1).p2)).y

is the member y in the object pointed to by p2 which is a member in the object pointed to by p1 , and 是构件y在对象通过指向p2这是在对象中的构件通过指向p1 ,并

(*((*((*p1).p2)).p3).z

is the member z in the object pointed to by p3 , which is a member in the object pointed to by p2 , which is a member in the object pointed to by p1 . p3指向的对象中的成员zzp2指向的对象中的成员, p2p1指向的对象中的成员。

It's entirely possible that this could be done with fewer parentheses, but they definitely help with understanding. 完全有可能用较少的括号来完成此操作,但是它们无疑有助于理解。

(*(*x).x).x

但是为什么,为什么呢???

Nested dereferencing ... 嵌套解引用

(*(*x).x).x

I Think that gets you there. 认为那能使您到达那里。 As to why you'd want to do such a thing ... ugly, ugly, ugly. 至于为什么你想做这样的事情……丑陋,丑陋,丑陋。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM