If you have an object, you'd use . to refer to its members.
If you have a pointer to an object, you'd use -> to dereference the pointer and access the object's members:
1 2 3
// these two are the same
ptr->bar;
(*foo).bar;
Example:
1 2 3 4 5 6 7
struct MyStruct { int foo; };
MyStruct obj;
obj.foo = 5; // we have an object, so use .
MyStruct* ptr = &obj; // ptr is a pointer
ptr->foo = 5; // since we have a pointer, use ->
what if i declare an obj with a pointer in struct,do i need -> when i refer to it?
It doesn't matter what is inside the struct. If the struct itself is being accessed with an object, you use the (.). If the struct itself is being accessed with a pointer, you use the arrow (->)
1 2 3 4 5 6 7 8 9 10 11
struct Example
{
int* pointer;
};
Example a;
a.pointer = whatever; // doesn't matter that 'pointer' is a pointer.
// 'a' is an object, therefore we use the dot
Example* b = &a;
b->pointer = whatever; // 'b' is a pointer, so we use ->