-4

A friend is taking a data structures course and I'm trying to explain the difference in the context of a linked list. I really should know this, but having focused on high level frameworks, I'm a little rusty. I know that "arrows are used with pointers", but that's it.

Essentially, in C, what's the difference between a dot and and an arrow when accessing a property of some variable? Also, because I'm in a particularly stupid mood, is this the same in C++, or did it change?

Moshe
  • 57,511
  • 78
  • 272
  • 425

3 Answers3

6

Normally the . is used when you have a structure to access directly, and the -> when you have a pointer to a structure and need to dereference it to access the structure.

a->b is syntactic sugar for (*a).b. It's the same in both C and C++.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
3

myPtr->someVariable is the same as (*myPtr).someVariable (dereferences myPtr, accesses the member). If you have *myPtr.someVariable it treats (myPtr.someVariable) as the pointer (access the member, dereference the whole thing as the pointer).

Raekye
  • 5,081
  • 8
  • 49
  • 74
1
pointersomething->member

is the same thing as

(*pointersomething).member

it exists as a shortcut as one could in theory do

*pointersomething.member

But the . operators have greater precedence than * operators, so the parenthesis are necessary- and thus the arrow shortcut.

Moshe
  • 57,511
  • 78
  • 272
  • 425
Max
  • 2,082
  • 19
  • 25