Question
While learning C pointers, I encountered the -> (arrow) operator without an explanation. Is it the equivalent of the . (dot) operator when working with a pointer to a structure? Please explain what it does and provide a code example.
Short Answer
The C arrow operator, ->, accesses a structure or union member through a pointer. It is shorthand for dereferencing the pointer and then using the dot operator: pointer->member is equivalent to (*pointer).member. You will learn when to use . versus ->, how the expression is evaluated, and how this appears in real C programs.
Concept
In C, a struct groups related values into one custom type. You use the dot operator (.) when you have the structure value itself:
struct Person person;
person.age = 30;
A pointer stores the address of another value. If you have a pointer to a structure, you must access the structure located at that address. The arrow operator (->) does this in one expression:
struct Person *person_ptr = &person;
person_ptr->age = 30;
This is exactly equivalent to:
(*person_ptr).age = 30;
The parentheses are essential because . has higher precedence than unary *. Without parentheses, *person_ptr.age would try to access age from person_ptr itself before dereferencing it, which is not what you want.
-> works with pointers to and objects. It accesses such as , , or .
Mental Model
Think of a structure value as a filing cabinet you are standing next to. With the dot operator, you open a labeled drawer directly:
person.age
A pointer is a slip of paper containing the cabinet's address. With the arrow operator, you follow the address to the cabinet and open its labeled drawer:
person_ptr->age
So -> means: go to the structure this pointer points to, then select this member.
Syntax and Examples
The core rule is:
pointer_to_struct->member
It means:
(*pointer_to_struct).member
Basic example
#include <stdio.h>
struct Book {
const char *title;
int pages;
};
int main(void) {
struct Book book = {"The C Programming Language", 272};
struct Book *book_ptr = &book;
printf("%s has %d pages.\n", book.title, book.pages);
printf("%s has %d pages.\n", book_ptr->title, book_ptr->pages);
book_ptr->pages = 275;
printf("Updated page count: %d\n", book.pages);
return 0;
}
book is a structure value, so it uses . contains the address of , so it uses .
Step by Step Execution
Consider this program:
#include <stdio.h>
struct Counter {
int value;
};
int main(void) {
struct Counter counter = {10};
struct Counter *ptr = &counter;
ptr->value += 5;
printf("%d\n", counter.value);
return 0;
}
Execution trace:
counteris created with one member:counter.valueis10.&counterproduces the memory address ofcounter.ptrstores that address, soptrpoints tocounter.ptr->valuemeans : dereference , producing , then select .
Real World Use Cases
The arrow operator is common whenever a C program passes structure pointers rather than copying whole structures.
-
Function parameters: A function receives a pointer so it can update the caller's structure.
void set_score(struct Player *player, int score) { player->score = score; } -
Dynamically allocated objects:
mallocreturns a pointer, so allocated structures use->.struct Node *node = malloc(sizeof *node); node->value = 42; -
Linked lists: Each node often stores a pointer to the next node.
current = current->next; -
Library APIs: Many C libraries use opaque structure pointers, such as file handles. For example,
FILE *is used with library functions, although its internal members should not be accessed directly. -
Configuration and state objects: Programs often pass a pointer to a shared application state or configuration structure to avoid copying it.
Real Codebase Usage
In production C code, -> is most often seen in functions that receive pointers to structures.
Update an object owned by the caller
struct Account {
int balance;
};
void deposit(struct Account *account, int amount) {
if (account == NULL || amount <= 0) {
return;
}
account->balance += amount;
}
This uses a guard clause to reject an invalid pointer or amount before accessing account->balance.
Read-only access with const
If a function should inspect a structure without changing it, use a pointer-to-const:
#include <stdio.h>
struct User {
const char *name;
int id;
};
void print_user(const struct User *user) {
if (user == ) {
;
}
(, user->name, user->id);
}
Common Mistakes
Using . on a pointer
This is incorrect because person_ptr is a pointer, not a struct Person value:
struct Person *person_ptr;
person_ptr.age = 30; /* Incorrect */
Use:
person_ptr->age = 30;
Forgetting parentheses with *
This is incorrect:
(*person_ptr.age) = 30; /* Incorrect */
It is parsed as *(person_ptr.age). Use either form below:
person_ptr->age = 30;
(*person_ptr).age = 30;
Dereferencing a NULL pointer
This has undefined behavior and often crashes:
;
person_ptr->age = ;
Comparisons
| Situation | Correct syntax | Reason |
|---|---|---|
| You have a structure value | person.age | person is the structure itself. |
| You have a pointer to a structure | person_ptr->age | person_ptr holds the structure's address. |
| Expanded arrow form | (*person_ptr).age | Dereference first, then access the member. |
| You have a pointer to a primitive value | *number_ptr | Primitive values have no named members. |
| You have a pointer to a pointer | (*person_ptr_ptr)->age | Dereference once to obtain a structure pointer, then use ->. |
Cheat Sheet
-
Use
.with a structure or union value:item.count. -
Use
->with a pointer to a structure or union:item_ptr->count. -
Equivalent expressions:
item_ptr->count (*item_ptr).count -
Parentheses are required in the expanded form:
(*ptr).member. -
ptr->memberrequiresptrto point to a valid structure or union object. -
Do not use
->throughNULL, a dangling pointer, or an uninitialized pointer. -
C has no normal object methods.
ptr->callback()is valid only whencallbackis a function-pointer member. -
A nested value often combines operators:
ptr->address.zip_code.
FAQ
What does -> mean in C?
It dereferences a pointer to a structure or union and accesses one of its members. ptr->member is shorthand for (*ptr).member.
When should I use . instead of -> in C?
Use . when your variable is the actual structure value. Use -> when your variable is a pointer to that structure.
Is ptr->member exactly the same as (*ptr).member?
Yes. They access the same member in the same object. The arrow form is shorter and easier to read.
Why are parentheses needed in (*ptr).member?
Because the dot operator binds more tightly than *. Parentheses ensure the pointer is dereferenced before selecting the member.
Can I use the arrow operator with arrays?
Not directly for an array itself. You can use it when a structure pointer has an array member, such as record_ptr->scores[0].
Can I call functions with -> in C?
Only if the selected structure member is a function pointer. Ordinary C functions are not structure members.
Mini Project
Description
Build a small linked-list traversal program. Each task is stored in a structure, and each structure points to the next task. This demonstrates a practical reason that C code frequently uses the arrow operator: navigating dynamically connected objects through pointers.
Goal
Print every task in a linked list and count the completed tasks using -> to access node members.
Requirements
Create a struct Task with a title, completion flag, and pointer to the next task.
Create three task variables and link them in order.
Write a function that receives a pointer to the first task.
Print each task title and its completion status.
Return the number of completed tasks.
Keep learning
Related questions
2D Array Loop Order and Cache Performance in C
Learn why swapping nested loops changes 2D array performance in C, using row-major memory layout, cache locality, and practical benchmarks.
Array-to-Pointer Conversion in C and C++ Explained
Learn what array-to-pointer conversion means in C and C++, how array decay works, and how it differs from a pointer to an array.
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.