Difference between "->" and "." in C

Hi everyone,

If one have defined a new typedef struct in C, the members of this new struct
will be accessed by "->" or "." in C?

I've read something about 'structure dereference", but unfortunately I don't understand it.

Can someone please explain me the difference between "->" and "." in C?

Best,
grima
They access members of the struct. If you just have an struct instance (object), you use the ".". If you have a pointer to a struct instance (pointer to an object), you use the "->".

As an example:
1
2
3
4
5
6
7
8
9
10
// example struct
typedef struct T {
   int a;
} myStruct;
// example variables
myStruct x; // a normal object
myStruct* p = &x; // a pointer to an object
// access members
x.a = 0; // access normal object's members with .
p->a = 0; // access pointer to object's members with -> 
Topic archived. No new replies allowed.