🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Pointer to Structure Arguments
Free C Programming Course for Embedded Systems · Structure and Union · Lecture 17
In the last lecture of this free C programming course we saw that a structure passed by value is copied. Now we fix both of its weaknesses at once. By passing a pointer to a structure to a function in C, the function works on the original object, can change it, and the only thing copied is an address.
This is the pattern behind almost every peripheral driver API you will meet in embedded C programming: uart_init(&cfg), spi_transfer(&msg), can_send(&frame). Learn it well and vendor HAL code starts to read like plain English.
Table of Contents
What You Will Learn
After this lecture you can:
- Declare a function that takes a pointer to a structure.
- Access members through a pointer with the arrow operator and with
(*p).member. - Modify the caller’s structure from inside a function.
- Protect read-only functions with
const. - Explain the speed and stack benefit compared with pass-by-value.
Prerequisites
You should know pointer basics (address-of, dereference), the pass-by-value behaviour from lecture 16, and how to declare and initialise a structure. Revisit the Pointers chapter of this free C tutorial if the dereference operator still feels unfamiliar.
Why Pass the Address of a Structure
There are two independent reasons to prefer a pointer over a copy.
- You want the function to change the caller’s data. A configuration helper that fills in default values is useless if it only edits a copy.
- You want to avoid copying a large object. An address is 4 bytes on a 32-bit MCU and 8 bytes on a 64-bit PC, however large the structure is.
One object, two names
The Arrow Operator
If p points to a structure, then *p is the structure and (*p).pin is its member. The parentheses are required because the dot operator binds tighter than the star. Writing this everywhere is tiresome, so C gives a shorthand: p->pin. The two forms mean exactly the same thing.
| You have | Access a member with |
|---|---|
A structure variable s | s.pin |
A pointer p to a structure | p->pin |
| A pointer, long form | (*p).pin |
Common slip: *p.pin does not mean what you hope. The dot binds first, so the compiler tries to use p.pin, and since p is a pointer, it fails to compile.
Complete Example: GPIO Configuration
This program models a GPIO pin configuration. apply_defaults receives a pointer and writes into the original. show only reads, so it takes a const pointer. At the end we print the size of a pointer and of a larger packet type to see the difference.
#include <stdio.h>
#include <stdint.h>
struct gpio_cfg {
char port; /* 'A', 'B', ... */
uint8_t pin;
uint8_t mode; /* 0 input, 1 output */
uint8_t speed;
};
struct big_packet {
uint8_t header[8];
uint8_t payload[56];
};
static void apply_defaults(struct gpio_cfg *c)
{
c->mode = 1;
c->speed = 2;
}
static void show(const struct gpio_cfg *c)
{
printf("P%c%u mode=%u speed=%u\n", c->port, (unsigned)c->pin,
(unsigned)c->mode, (unsigned)c->speed);
}
int main(void)
{
struct gpio_cfg led = { 'A', 5, 0, 0 };
struct gpio_cfg *p = &led;
show(&led);
apply_defaults(&led);
show(p);
(*p).pin = 6; /* same as p->pin */
show(p);
printf("sizeof(struct gpio_cfg *) = %zu\n", sizeof(struct gpio_cfg *));
printf("sizeof(struct big_packet) = %zu\n", sizeof(struct big_packet));
return 0;
}
Build and run:
gcc -std=c11 -Wall -Wextra -o ptr_struct ptr_struct.c
./ptr_struct
Expected output on a 64-bit Linux machine:
PA5 mode=0 speed=0
PA5 mode=1 speed=2
PA6 mode=1 speed=2
sizeof(struct gpio_cfg *) = 8
sizeof(struct big_packet) = 64
What the output proves
The second line shows the defaults appeared in led after apply_defaults returned. The function really changed the caller’s object. On a 32-bit microcontroller the pointer size line would print 4.
Using const for Read-Only Access
A pointer parameter gives the function power to modify the structure. That power is not always wanted. Declaring the parameter as const struct gpio_cfg * tells the compiler, and every reader, that the function only looks. If you then try to assign through it, the compiler stops you. In larger firmware this single keyword catches whole categories of bugs, and it documents intent better than any comment.
| Parameter type | Function may read | Function may write | Use for |
|---|---|---|---|
struct gpio_cfg | Yes | Its own copy only | Small read-only data |
struct gpio_cfg * | Yes | Yes, the original | Init and update functions |
const struct gpio_cfg * | Yes | No | Print, validate, send |
Step by Step: What the Call Does
Trace apply_defaults(&led) one move at a time. First the caller evaluates &led, which is the address of the real structure. Second, that address is copied into the parameter c. Third, the statement c->mode = 1 follows the address and writes into led.mode. Finally the function returns, c disappears, and the structure it pointed to stays alive in the caller. Only the pointer was a temporary; the data never moved.
This is still call by value, because the pointer itself is copied. What makes it powerful is that the copied pointer holds the same address as the original, so both lead to one object. If the function assigned a different address to c inside its body, the caller’s pointer would not change, because that assignment only touches the local copy of the pointer.
Size Comparison
Passing struct big_packet by value copies 64 bytes on every call. Passing a pointer copies 4 or 8 bytes. In a function called thousands of times per second, or from an interrupt handler with a tiny stack, this difference decides whether the design is practical. As a bonus, the pointer version also lets you avoid duplicate buffers in RAM.
Real-World Use Cases
- Peripheral init: the caller fills a configuration structure, the driver reads it through a const pointer.
- Status reporting: a driver fills a status structure supplied by the caller.
- Message queues: tasks pass pointers to message structures instead of copying payloads.
Common Mistakes
| Mistake | Result | Fix |
|---|---|---|
Passing led instead of &led | Compile error or wrong type | Pass the address |
| Using the dot operator on a pointer | Compile error | Use the arrow |
| Passing a NULL or uninitialised pointer | Crash or hard fault | Check for NULL at the top of public functions |
| Forgetting const on read-only functions | Accidental writes go unnoticed | Add const |
| Keeping the pointer after the structure goes out of scope | Dangling pointer | Never store addresses of locals |
Best Practices
- Use a pointer parameter for any structure that is larger than a couple of machine words.
- Use
constby default and remove it only when the function must write. - Validate pointer arguments in functions that are part of a public API.
- Document who owns the memory: the caller or the callee.
- Keep the arrow operator for pointers and the dot for variables, so the code tells you which one you hold.
Summary and Conclusion
Key Takeaways
- Passing the address lets the function modify the caller’s structure.
- Only the address is copied, so cost does not grow with structure size.
- Use
->to reach members through a pointer. - Use
constto make read-only intent explicit and compiler-checked.
Passing a pointer to a structure to a function in C is the standard tool for efficient, modifiable access, and you will use it in every embedded project. In the next lecture we go the other direction and learn how a function can hand a whole structure back to its caller.
Suggested Images
- Diagram of a pointer arrow from a function parameter to the caller’s structure. ALT: pointer to structure passed to function in C diagram, free C programming course
- Terminal screenshot of the GPIO demo output. ALT: embedded C programming arrow operator structure pointer output, free embedded C course
Frequently Asked Questions
What does the arrow operator do in C?
It dereferences a pointer to a structure and selects a member in one step, so p->x equals (*p).x.
Can a function change my structure if I pass a pointer?
Yes, unless the parameter is declared const.
Is passing a pointer faster than passing the structure?
For anything beyond a few bytes, yes, because only an address is copied.
What is a const struct pointer?
A pointer through which the structure can be read but not modified.
Do I need to check a structure pointer for NULL?
In public driver functions it is good practice, since a NULL dereference usually triggers a fault.
Why does an embedded C programming course teach this early?
Vendor drivers and RTOS APIs are built from functions that take pointers to configuration and message structures.
Continue the Free Embedded C Course
Next, learn how to return a structure from a function in this free C tutorial for embedded systems.

2 Comments