LOGBOOK

HELP

Quiz Entry - updated: 2026.07.10

What's the difference between -> and . for struct access?

Use . when you hold the struct itself (p.x) and -> when you hold a pointer to it (ptr->x); ptr->x is just shorthand for (*ptr).x.

struct point { int x, y; };

struct point p = {10, 20};
struct point *ptr = &p;

// Direct access (variable)
p.x = 5;

// Pointer access
// Preferred
ptr->x = 5;
// Equivalent but uglier
(*ptr).x = 5;

Why -> exists:

  • *ptr.x doesn't work because . has higher precedence than *
  • *ptr.x is parsed as *(ptr.x) which is wrong
  • (*ptr).x works but is clunky
  • ptr->x is shorthand for (*ptr).x

Chaining:

struct node {
    int value;
    struct node *next;
};

// Follow chain of pointers
node->next->next->value
// Equivalent to: (*((*node).next)).next)->value  // Yuck!

From Quiz: REVE1 / C Programming | Updated: Jul 10, 2026