Skip to main content

Passing Structures to Functions

You can pass a structure to a function the same way you pass any variable. There are three approaches:
  1. Pass by Value
  2. Pass by Reference (using pointer)
  3. 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.
Output:

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 (->).
Output:

3. Return a Structure from a Function


Comparison

Best practice: For large structures, always prefer passing by reference using a pointer. Use const struct Type *ptr if you want to read without modifying.