Menu
Coddy logo textTech

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->member

This 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 icon

Challenge

Easy

Create a C program that demonstrates using the arrow operator to access struct members through pointers. Your program should:

  1. Define a struct named Book with the following members:
    • An integer id to store the book ID
    • A character array title with size 40 to store the book title
    • A character array author with size 30 to store the author name
    • A float price to store the book price
    • An integer pages to store the number of pages
  2. In the main function, create a Book variable named myBook
  3. Declare a pointer to a Book struct named bookPtr
  4. Assign the address of myBook to bookPtr
  5. 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
  6. 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;
  7. 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]
  8. 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->member

This 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;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow