-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
  • 56,717
  • 76
  • 267
  • 423

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
  • 210,715
  • 34
  • 410
  • 462
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
  • 4,953
  • 8
  • 46
  • 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
  • 56,717
  • 76
  • 267
  • 423
Max
  • 1,964
  • 18
  • 23