🚀 Learn BLE on Linux (BlueZ) – Programming Part 1
Hands-on Bluetooth Low Energy course. Special price: ₹399
Enroll Now on Udemy →Structure Pass By Value
Free C Programming Course for Embedded Systems · Structure and Union · Lecture 16
Welcome to lecture 16 of our free C programming course. Today we study passing a structure to a function in C, the most basic way to hand a group of related values to a function. If you are following this free embedded C course to prepare for firmware work, this lecture matters more than it looks: every driver API that takes a configuration or a sensor reading is built on the ideas below.
Structures group different types under one name. The question we answer here is simple: when you give a structure to a function, what does the function actually receive, and can it change your original data?
Table of Contents
What You Will Learn
By the end of this lecture you can:
- Pass individual structure members to a function and explain why this becomes clumsy.
- Pass a whole structure variable to a function and read its members with the dot operator.
- Explain why the function works on a copy and cannot change the caller’s structure.
- Estimate the cost of copying a structure on a small microcontroller.
- Decide when pass-by-value is the right choice and when it is not.
Prerequisites
You should be comfortable with defining a structure type, declaring structure variables, initialising them with braces, and using the dot operator. These were covered in lectures 1 to 15 of this chapter. You also need the basics of functions: prototypes, parameters and return types.
Passing Structure Members vs the Whole Structure
Suppose your firmware reads a temperature sensor and you want a function that prints the reading. There are two natural designs.
Design 1: Pass Each Member Separately
You write a function with one parameter per member and call it as show(r.id, r.temp_x10, r.millivolts). This works because each member is an ordinary integer. The names of the formal parameters can be anything; they need not match the member names. The function sees plain values and knows nothing about the structure.
The problem appears when the structure grows. A real peripheral configuration can have ten or more fields. A ten-argument function is hard to read, easy to call with arguments in the wrong order, and every new field forces you to edit every call site. Worse, the relationship between the values is lost: the function signature no longer tells you these values belong together.
Design 2: Pass the Whole Structure
C lets you pass a structure variable directly, exactly like an int. The parameter is declared with the structure type, and the call site passes the variable name. One parameter carries every member, and adding a field later does not change the function signature.
Members one by one versus whole structure
| Aspect | Pass members | Pass whole structure |
|---|---|---|
| Parameters | One per member | One in total |
| Adding a field | Change every prototype and call | No signature change |
| Readability | Drops as members grow | Stays clear |
| Relationship kept | No | Yes |
| Caller’s data changed | No (copies of values) | No (copy of structure) |
Complete Example: Sensor Reading
The program below defines a sensor reading with fixed-width types, which is how embedded code is normally written. Temperature is stored in tenths of a degree so no floating point is needed. We pass the whole structure to print_reading, then pass it to try_calibrate, which modifies its parameter.
#include <stdio.h>
#include <stdint.h>
struct sensor_reading {
uint8_t id;
int16_t temp_x10; /* temperature in tenths of a degree C */
uint16_t millivolts;
};
static void print_reading(struct sensor_reading r)
{
printf("sensor %u: %d.%d C, %u mV\n",
(unsigned)r.id, r.temp_x10 / 10, r.temp_x10 % 10,
(unsigned)r.millivolts);
}
static void try_calibrate(struct sensor_reading r)
{
r.temp_x10 += 25; /* changes only the local copy */
printf("inside try_calibrate: %d.%d C\n", r.temp_x10 / 10, r.temp_x10 % 10);
}
int main(void)
{
struct sensor_reading s1 = { 1, 235, 3300 };
struct sensor_reading s2 = { 2, 198, 3290 };
print_reading(s1);
print_reading(s2);
try_calibrate(s1);
print_reading(s1); /* unchanged */
return 0;
}
Save it as pass_value.c, then build and run:
gcc -std=c11 -Wall -Wextra -o pass_value pass_value.c
./pass_value
Expected output:
sensor 1: 23.5 C, 3300 mV
sensor 2: 19.8 C, 3290 mV
inside try_calibrate: 26.0 C
sensor 1: 23.5 C, 3300 mV
Read the last two lines carefully
Inside try_calibrate the temperature became 26.0. Back in main it is still 23.5. The function changed its own private copy, and the original s1 was never touched.
Important: the structure type must be declared before the function prototype and the function body, normally at file scope near the top or in a header. If the type is only declared inside main, the functions cannot name it as a parameter type.
What Really Happens: The Copy
Unlike an array, the name of a structure variable is not converted into a pointer. The name stands for the whole object. When you write print_reading(s1), the compiler arranges for every byte of s1 to be copied into the parameter r. Depending on the target ABI, the copy goes onto the stack or into registers for very small structures. This is called call by value.
Call by value: two separate objects in memory
This behaviour is a feature. It protects the caller from accidental damage: a function that only receives a copy cannot corrupt your data. It is also the reason a function written like try_calibrate is pointless. If you want a function to modify the caller’s structure, you must pass its address, which is the subject of the next lecture.
Members That Are Arrays
One surprising detail: if a structure contains an array member, the whole array is copied with the structure. That is the one place in C where an array is copied by value. It is legal, but it makes the copy larger, which brings us to cost.
Cost of Passing a Structure to a Function in C
On a desktop, copying 12 bytes is invisible. On a Cortex-M0 with 4 KB of RAM and a small stack, it is a design decision. Each by-value call copies the full size of the structure onto the stack, and that memory is released only when the function returns. If your function calls another function that also takes the structure by value, the copies stack up.
| Structure size | Copy cost | Typical advice |
|---|---|---|
| Up to about 8 bytes | Often fits in registers | By value is fine |
| 9 to 32 bytes | A few load/store instructions | By value acceptable if rarely called |
| Over 32 bytes | Noticeable stack and time | Prefer a pointer |
| Contains big arrays | Whole array copied | Always use a pointer |
These size bands are a rule of thumb, not a language rule. Always check the generated code and your stack budget on the real target.
Where This Appears in Real Firmware
- Printing or logging a small sample record, such as one ADC reading with a timestamp.
- Passing a tiny coordinate or fixed-point pair to a math helper.
- Sending a read-only snapshot of state to a function that must not alter it.
Common Mistakes
| Mistake | What goes wrong | Fix |
|---|---|---|
| Expecting the function to update the caller’s structure | Changes vanish on return | Pass a pointer, or return the modified structure |
| Declaring the structure inside main only | Compile error in the function prototype | Declare the type at file scope |
| Passing a large structure by value in an interrupt handler | Stack overflow risk | Pass a pointer |
| Forgetting that two structure types with identical members are still different types | Compile error when mixing them | Use one shared type definition |
| Printing a uint8_t with %d without a cast | Warning or wrong width on some platforms | Cast to unsigned and use %u |
Best Practices
- Use pass-by-value only for small structures that the function treats as read-only input.
- Put the structure definition in a header so every file agrees on the layout.
- Use fixed-width integer types from
stdint.hin embedded structures. - Name parameters clearly; avoid reusing a member name as a parameter name when it hurts readability.
- Check the compiler’s stack usage report for functions that take structures by value.
Summary and Conclusion
Key Takeaways
- You can pass members individually or the whole structure variable.
- A structure variable name is not a pointer; passing it copies every byte.
- The function modifies only its copy, so the caller’s data is safe.
- Copy cost grows with size; large structures belong behind pointers.
Passing a structure to a function in C by value is simple and safe, and it is the right default for small, read-only records. In the next lecture of this free C tutorial we pass a pointer to the structure so the function can work on the original and avoid the copy.
Suggested Images
- Diagram of two stack frames showing the copy of a structure. ALT: passing structure to function in C call by value copy diagram, free C programming course
- Screenshot of the terminal output of the demo. ALT: embedded C programming structure pass by value program output, free embedded C course
Frequently Asked Questions
Can I pass a structure to a function in C without a pointer?
Yes. C passes structures by value, so the function receives a full copy of the variable.
Will the original structure change if the function modifies its parameter?
No. Only the copy changes. Use a pointer parameter when you need to modify the original.
Is passing a structure by value slow?
It costs time and stack proportional to the structure size. For a few bytes it is negligible; for large structures it is wasteful.
Does the copy include array members?
Yes. An array inside a structure is copied along with the rest of the structure.
Must the parameter names match the member names?
No. Parameter names are independent of member names.
Why is this topic important for embedded C programming?
Stack and RAM are limited on microcontrollers, so you must know exactly what each call copies.
Keep Learning with the Free Embedded C Course
Continue with pointers to structures in the next lecture of this free C tutorial for embedded systems.

2 Comments