Course contentsShow
← C
Lesson 616 of 1,2199. Structures and UnionsFree lesson

The arrow operator `->` syntax

The arrow operator -> syntax

What you'll learn: How to cleanly access a structure's members when you are working with a pointer to that structure.

The problem with dots and stars

In C, you already know that you use the dot operator (.) to access a field inside a struct. For example, if you have myCar.speed, you are looking directly at the "speed" property of the myCar object.

However, things get messy when you deal with pointers. If you have a pointer to a struct, you first have to "follow" the pointer to find the actual data (dereferencing) and then grab the field. Because of how C handles math priority, you would have to write something clunky like (*ptr).speed. The parentheses are mandatory because, without them, C would try to look for a pointer named ptr.speed, which doesn't exist.

Writing (*ptr).member over and over is exhausting and hard to read.

Enter the Arrow ->

To make life easier, C provides a "shortcut" called the arrow operator. It combines the act of following the pointer and accessing the member into one clean motion.

Think of it this way:

  • Use the dot (.) when you are holding the actual box (the struct).
  • Use the arrow (->) when you are holding a map (the pointer) that tells you where the box is.

See it in action

Here is how the arrow operator simplifies your code:

#include <stdio.h>

struct Player {
    char name[20];
    int score;
};

int main() {
    struct Player p1 = {"Alice", 500};
    struct Player *ptr = &p1;

    // The clunky way (dereferencing + dot)
    printf("Player: %s\n", (*ptr).name);

    // The elegant way (the arrow operator)
    // This says: "Follow ptr to the struct, then get 'score'"
    ptr->score = 600; 

    printf("%s's new score: %d\n", ptr->name, ptr->score);

    return 0;
}

Why it matters

You will use the arrow operator constantly when building complex data structures like linked lists or trees, or when passing large structs into functions via pointers to save memory. Whenever you see ->, just remember: "Follow the pointer and grab the value."

Key takeaway

Use ptr->member as a readable shortcut for (*ptr).member when working with pointers to structures.