A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
The arrow operator -> is syntactic sugar for dereferencing a pointer and then accessing a member. p->x is identical to (*p).x. The parentheses in (*p).x are mandatory because the dot operator has higher precedence than the dereference operator. Without them, *p.x parses as *(p.x), which tries to access member x of p and then dereference it. The arrow operator eliminates the parentheses and the precedence confusion. It exists purely for convenience and readability.
#include <stdio.h>#include <string.h>typedef struct {char name[32];int value;} Item;int main(void) {Item item = {"Sword", 50};Item *ptr = &item;printf("Dot: %s = %d\n", item.name, item.value);printf("Arrow: %s = %d\n", ptr->name, ptr->value);printf("Deref: %s = %d\n", (*ptr).name, (*ptr).value);ptr->value = 75;printf("\nAfter ptr->value = 75:\n");printf("item.value = %d\n", item.value);return 0;}
All three access methods produce the same result. The arrow and the explicit dereference modify the original struct because they go through the pointer. The dot operator accesses the struct value directly. In practice, you will use -> almost exclusively when working with pointers, which is most of the time in C data structures. Linked list nodes, tree nodes, hash table entries: all accessed through pointers, all using ->.
The arrow operator is a small convenience with a big readability payoff. Use it consistently with pointer-to-struct access. It signals to the reader that you are working through a pointer, not with a value. Next: unions, where all members share the same memory and only one is active at a time.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.