Linked list equivalency

Why is (*x).y is not equivalent to *x.y?

is (*x).y and x->y equivalent?
Last edited on
They are both equivalent and unequivalent
I don't understand..

So (*x).y and x-> y is equivalent and unequivalent at the same time?
well you do live up to your name :p
Ignore him.

It's about operator precedence and the order in which these operators are performed.
Parentheses always displays order over precedence rules.
(*x).y will de-reference x, and then access it's member variable called y;
*x.y doesn't have any parentheses. So C++ looks at its precedence rules. . has precedence over *, so it will do (x.y) and then de-reference that result.

Yes, these last two are equivalent.

(*x) will de-reference x, and .y will access the member variable y; x->y is just "syntactic sugar" for (*x).y
Last edited on
So conclusively (*x).y is equivalent to x->y and is not equivalent to *x.y?
Am I on the right track?
Disregard das2's comments.

Check out this page for more information about operator precedence: http://en.cppreference.com/w/cpp/language/operator_precedence
supernoob, please completely disregard anything and EVERYTHING he has to say
Gotcha

Side question:

a.b.c=num;

(*d).e->f = 10;

Structure variable: c
Structure component: a and b
Pointer variable: d and e

is this correct?
So for this one: a.b.c=num;, both of those operators are the same.
The . goes left-to-right when there are multiples of them right next to each other.
You think of it like this: a has a member variable b and b has a member variable c. It first accesses a.b, then accesses b.c

Um, the second one looks weird. Ok. So, there are parenthesis, so that has to be done first.
(*d) de-referecnes d. And . and -> have the same precedence, and there are not parentheses or anything to tell which one to do first, so it is done lef-to-right.

But please note that not all operators go left-to-right. There are some that go right-to-left when there are no parenteses or anything to help the compiler discern in which order they should be done.

For a.b.c=num;, all 3 are objects/structures.

For (*d).e->f = 10;, d and e are pointers, and f is a pointer.

Here's some code that will display this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
struct _c {
	int i;
};

struct _b {
	_c c; //has 'c' as a component
};

struct _a
{
	_b b; //this has 'b' as a component
};

struct _f
{
	int io;
};

struct _e
{
	_f *f; //this has 'f' has a pointer (still a component)
};

struct _d
{
	_e *e; //this has 'e' as a pointer (still a component)
};


int main()
{
	_a a; //'a' is a variable

	a.b.c; //this is allowed

	_d *d;// 'a' is a variable

	(*d).e->f; //this is allowed

}
Last edited on
The thing is my instructor differentiates structure variables and structure components.

Yes I know that a.b.c=num all are structures, but which one's the component and which one's the variable?
'a' is the variable declared in 'main', and 'b' and 'c' are components.
That cleared out so much. Thanks!
Topic archived. No new replies allowed.