Passing Structures to Functions
You can pass a structure to a function the same way you pass any variable. There are three approaches:- Pass by Value
- Pass by Reference (using pointer)
- Return a structure from a function
1. Pass by Value
A copy of the structure is sent to the function. Changes made inside the function do not affect the original.2. Pass by Reference (Using Pointer)
The address of the structure is sent. Changes made inside the function do affect the original. Members are accessed using the arrow operator (->).
3. Return a Structure from a Function
Comparison
| Method | Copies Data | Modifies Original | Speed |
|---|---|---|---|
| Pass by Value | Yes | No | Slower |
| Pass by Reference | No | Yes | Faster |
| Return struct | Yes | — | Moderate |
const struct Type *ptr if you want to read without modifying.