The Arrow Operator (->)
Part of the Logic & Flow section of Coddy's C journey — lesson 45 of 63.
When working with pointers to structs, accessing the struct members requires a special syntax. While you could use the dereference operator and dot notation together, C provides a more convenient operator specifically for this purpose: the arrow operator (->).
The arrow operator allows you to access struct members through a pointer in a clean, readable way:
ptr->memberThis is equivalent to writing (*ptr).member, but much more concise.
Here's how it works in practice:
struct Point p1 = {10, 20};
struct Point *ptr = &p1;
// Using the arrow operator
printf("x = %d, y = %d\n", ptr->x, ptr->y);
// This is the same as:
printf("x = %d, y = %d\n", (*ptr).x, (*ptr).y);The arrow operator makes your code cleaner and easier to read, especially when working with complex data structures. It's the standard way to access struct members through pointers in C programming.
Challenge
EasyCreate a C program that demonstrates using the arrow operator to access struct members through pointers. Your program should:
- Define a
structnamedBookwith the following members:- An integer
idto store the book ID - A character array
titlewith size 40 to store the book title - A character array
authorwith size 30 to store the author name - A float
priceto store the book price - An integer
pagesto store the number of pages
- An integer
- In the main function, create a
Bookvariable namedmyBook - Declare a pointer to a
Bookstruct namedbookPtr - Assign the address of
myBooktobookPtr - Read the following input values and assign them to the struct members using the arrow operator through the pointer:
- Read an integer for the ID and assign it using
bookPtr->id - Read a string for the title and assign it using
bookPtr->title - Read a string for the author and assign it using
bookPtr->author - Read a float for the price and assign it using
bookPtr->price - Read an integer for the pages and assign it using
bookPtr->pages
- Read an integer for the ID and assign it using
- After reading all values, perform the following operations using the arrow operator:
- Apply a 10% discount to the price:
bookPtr->price = bookPtr->price * 0.9; - Add 50 bonus pages:
bookPtr->pages += 50;
- Apply a 10% discount to the price:
- Print the book information using the arrow operator to access each member in this exact format:
Book Information:ID: [id]Title: [title]Author: [author]Price: [price]Pages: [pages]
- Calculate and print the price per page using the arrow operator in the format:
Price per page: [price_per_page]
This challenge tests your understanding of the arrow operator for accessing struct members through pointers. You'll practice using ptr->member syntax for both reading from and writing to struct members, demonstrating that it's equivalent to (*ptr).member but much more convenient and readable.
Cheat sheet
The arrow operator (->) provides a convenient way to access struct members through a pointer:
ptr->memberThis is equivalent to (*ptr).member but more concise and readable.
Example usage:
struct Point p1 = {10, 20};
struct Point *ptr = &p1;
// Using the arrow operator
printf("x = %d, y = %d\n", ptr->x, ptr->y);
// This is the same as:
printf("x = %d, y = %d\n", (*ptr).x, (*ptr).y);Try it yourself
#include <stdio.h>
#include <string.h>
// TODO: Define the Book struct here
int main() {
// TODO: Create a Book variable named myBook
// TODO: Declare a pointer to Book named bookPtr
// TODO: Assign the address of myBook to bookPtr
// Read input values
int id;
char title[40];
char author[30];
float price;
int pages;
scanf("%d", &id);
scanf("%s", title);
scanf("%s", author);
scanf("%f", &price);
scanf("%d", &pages);
// TODO: Assign the input values to struct members using arrow operator
// TODO: Apply 10% discount to price using arrow operator
// TODO: Add 50 bonus pages using arrow operator
// TODO: Print book information using arrow operator
// TODO: Calculate and print price per page using arrow operator
return 0;
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Pointers Fundamentals
What is a Pointer?Declaring PointersThe Address-Of Operator (&)The Dereference Operator (*)NULL PointersRecap: Pointer Basics4Project: Simple Text Utility
Project OverviewCounting Characters2Pointers and Arrays
Array Names as PointersArray Elements - PointersPointer ArithmeticComparing PointersRecap: Pointer Array Traversal5Pointers and Functions
Pass-by-ValuePassing Pointers to FunctionsModifying Vars via PointersA Classic Example: SwapPassing Arrays to FunctionsRecap: Function Pointer Args8Structs and Pointers
Pointers to StructsThe Arrow Operator (->)Passing Structs by ValuePassing Struct PointersDynamic Allocation of StructsRecap: Modifying Struct - Ptr11Final Recap Challenges
Recap: Dynamic String ConcatRecap: Array of StructsRecap: Word Frequency Counter3Character Arrays and Strings
Strings as char ArraysThe Null TerminatorString Input with scanfUsing strlen()Using strcpy()Using strcat()Using strcmp()Recap: Basic String Functions