I have looked at some resources to tell me how ->
and .
are different, but they seem to do the same thing. Does ->
act like a dot operator on a struct?
.
is used when you have a struct, and ->
is used when you have a pointer to a struct. The arrow is a short form for dereferencing the pointer and then using .
: p->field
is the same as (*p).field
.
They are almost the same thing. The only difference is that "->" takes a pointer to a struct on the left side while "." takes a struct; "->" deferences (i.e. follows) the pointer before accessing the struct member. So,
struct foo bar;
bar.x = 0;
is the same as:
struct foo bar;
struct foo *diddly = &bar;
diddly->x = 0;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With