Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do the "->" and "." member access operations differ in C

Tags:

c

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?

like image 573
TheTuxedo Avatar asked Aug 06 '10 13:08

TheTuxedo


2 Answers

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

like image 131
Graeme Perrow Avatar answered Oct 16 '22 23:10

Graeme Perrow


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;
like image 28
fool4jesus Avatar answered Oct 16 '22 22:10

fool4jesus