简体   繁体   中英

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?

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.

Well x -> x is equivalent to (*x).x So you just do that twice:

(*(*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->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).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

(*((*((*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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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