Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Why does the arrow (->) operator in C exist?

The dot (.) operator is used to access an individual of a struct, while the arrow operator (->) in C is used to access a member of a struct which is referenced by the pointer in question.

The pointer itself does not have any members which could be accessed with the dot operator (it's actually only a number describing a location in virtual memory so it doesn't have any members). So, there would be no ambiguity if we just defined the dot operator to automatically dereference the pointer if it is used on a pointer (information which is known to the compiler at compile time afaik).

So why have the language creators decided to make things more complicated by adding this seemingly unnecessary operator? What is the big design decision?
by

1 Answer

sandhya6gczb
C also does a good job at not making anything ambiguous.

Sure the dot could be overloaded to mean both things, but the arrow makes sure that the programmer knows that he's operating on a pointer, just like when the compiler won't let you mix two incompatible types.

Login / Signup to Answer the Question.