0

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

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.

3
  • In C# and Java (as of Java 8) -> is used in lambdas – yitzih Sep 28 '16 at 22:39
  • 2
    @yitzih In C# at least, -> remains a pointer dereference like C++ when you compile as unsafe. Lambdas are =>. – Kevin Fee Sep 28 '16 at 22:43
  • @yitzih, good catch. I'm so used to C# and Javascript, which both use => for lambdas, that I forgot Java uses -> for that! – Erik Eidt Sep 29 '16 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.