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
}
| |