Menu
Coddy logo textTech

Pointers to Structs

Part of the Logic & Flow section of Coddy's C journey — lesson 44 of 63.

Now that you're comfortable working with structs, it's time to combine them with another powerful C concept: pointers. Just as you can create pointers to basic data types like int or char, you can also create pointers that hold the address of a struct variable.

The syntax for declaring a pointer to a struct follows the same pattern as other pointer declarations:

struct StructureName *pointerName;

For example, using our familiar Point struct:

struct Point p1 = {10, 20};
struct Point *ptr;

This creates a pointer named ptr that can store the address of a Point struct. To make the pointer actually point to our struct variable, you use the address-of operator:

ptr = &p1;

Now ptr holds the memory address of the p1 struct variable. This opens up new possibilities for working with structs, especially when passing them to functions or managing memory dynamically. Pointers to structs are essential for building more complex data structures and writing efficient C programs.

challenge icon

Challenge

Easy

Create a C program that demonstrates working with pointers to structs. Your program should:

  1. Define a struct named Product with the following members:
    • An integer id to store the product ID
    • A character array name with size 25 to store the product name
    • A float price to store the product price
    • An integer quantity to store the available quantity
  2. In the main function, create a Product variable named item and initialize it with the following values:
    • ID: 501
    • Name: "Laptop"
    • Price: 899.99
    • Quantity: 15
  3. Declare a pointer to a Product struct named itemPtr
  4. Assign the address of the item variable to itemPtr using the address-of operator
  5. Print the original product information by accessing the struct members directly (using the dot operator) in this exact format:
    • Original Product Info:
    • ID: [id]
    • Name: [name]
    • Price: [price]
    • Quantity: [quantity]
  6. Print the same product information by accessing the struct members through the pointer (using the dereference operator and dot notation: (*itemPtr).member) in this exact format:
    • Product Info via Pointer:
    • ID: [id]
    • Name: [name]
    • Price: [price]
    • Quantity: [quantity]
  7. Verify that the pointer is correctly pointing to the struct by printing the memory addresses in this exact format:
    • Address of item: [address]
    • Value of itemPtr: [address]
    • Address verification: [result]

For the address verification, print Match if the addresses are the same, or No Match if they are different.

This challenge tests your understanding of pointer-to-struct declaration, address assignment using the address-of operator, and accessing struct members through pointers using the dereference operator combined with the dot notation. You'll also verify that the pointer correctly holds the address of the struct variable.

Cheat sheet

You can create pointers to structs using the same syntax as other pointer declarations:

struct StructureName *pointerName;

To make a pointer point to a struct variable, use the address-of operator:

struct Point p1 = {10, 20};
struct Point *ptr;
ptr = &p1;

Access struct members through a pointer using the dereference operator and dot notation:

(*ptr).member

Try it yourself

#include <stdio.h>
#include <string.h>

// TODO: Define the Product struct here

int main() {
    // TODO: Write your code here
    // 1. Create and initialize the Product variable 'item'
    // 2. Declare a pointer to Product named 'itemPtr'
    // 3. Assign the address of 'item' to 'itemPtr'
    // 4. Print original product info using dot operator
    // 5. Print product info via pointer using (*itemPtr).member
    // 6. Print and verify addresses
    
    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