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
EasyCreate a C program that demonstrates working with pointers to structs. Your program should:
- Define a
structnamedProductwith the following members:- An integer
idto store the product ID - A character array
namewith size 25 to store the product name - A float
priceto store the product price - An integer
quantityto store the available quantity
- An integer
- In the main function, create a
Productvariable nameditemand initialize it with the following values:- ID: 501
- Name: "Laptop"
- Price: 899.99
- Quantity: 15
- Declare a pointer to a
Productstruct nameditemPtr - Assign the address of the
itemvariable toitemPtrusing the address-of operator - 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]
- 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]
- 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).memberTry 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;
}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