0

As the title states, what is the difference between -> and . I thought they were the same thing?

johnson
  • 109

1 Answers1

6

These are different operators, though related.

In C, . is field selection, and -> combines pointer dereference and field selection. So a->b is equivalent to (*a).b or more correctly (*(a)).b.

Many languages don't have both . and ->. In C# and Java, for example, which don't have ->, the . operator does both dereference and field selection as in -> in C. However, in C# and Java, . also does selection from a namespace, which does not involve dereferencing (more like C++'s :: operator).


Note: C# does have an unsafe construct that allows for unsafe/raw pointers, and also does actually have the -> operator to work with them. However, you won't encounter -> unless you're working in unsafe code.

Erik Eidt
  • 33,747