The advantage is in the size being passed: when you pass a large struct
, the compiler generates code to make a copy of that struct
if you pass it by value. This wastes CPU cycles, and may create a situation when your program runs out of stack space, especially on hardware with scarce resources, such as embedded microcontrollers.
When you pass a struct
by pointer, and you know that the function must not make modifications to it, declare the pointer const
to enforce this rule:
void take_struct(const struct arg_struct *data) {
data->field = 123; // Triggers an error
}