0
0
CHow-ToBeginner · 3 min read

How to Pass Structure to Function in C: Syntax and Examples

In C, you can pass a structure to a function either by value using structName or by reference using a pointer structName*. Passing by value copies the entire structure, while passing by pointer allows the function to modify the original structure.
📐

Syntax

To pass a structure to a function, you can use either of these two ways:

  • Pass by value: The function receives a copy of the structure.
  • Pass by pointer: The function receives the address of the structure, allowing it to modify the original.

Example syntax:

void functionName(struct StructType param);  // pass by value
void functionName(struct StructType *param); // pass by pointer
c
struct Point {
    int x;
    int y;
};

// Pass by value
void printPoint(struct Point p) {
    printf("Point coordinates: (%d, %d)\n", p.x, p.y);
}

// Pass by pointer
void movePoint(struct Point *p, int dx, int dy) {
    p->x += dx;
    p->y += dy;
}
💻

Example

This example shows how to pass a structure by value and by pointer to functions. It prints the point and then moves it by changing its coordinates.

c
#include <stdio.h>

struct Point {
    int x;
    int y;
};

void printPoint(struct Point p) {
    printf("Point coordinates: (%d, %d)\n", p.x, p.y);
}

void movePoint(struct Point *p, int dx, int dy) {
    p->x += dx;
    p->y += dy;
}

int main() {
    struct Point pt = {5, 10};
    printPoint(pt);           // Pass by value
    movePoint(&pt, 3, -4);   // Pass by pointer
    printPoint(pt);           // Print after moving
    return 0;
}
Output
Point coordinates: (5, 10) Point coordinates: (8, 6)
⚠️

Common Pitfalls

Common mistakes when passing structures to functions include:

  • Passing large structures by value can be inefficient because it copies the whole structure.
  • Forgetting to use & when passing a pointer to the function, causing a type mismatch.
  • Modifying a structure inside a function passed by value does not affect the original.

Example of wrong and right ways:

c
#include <stdio.h>

struct Data {
    int value;
};

// Wrong: passing pointer but forgetting & in call
void updateValue(struct Data *d) {
    d->value = 100;
}

int main() {
    struct Data data = {50};
    // updateValue(data); // Error: expected pointer, got struct
    updateValue(&data);   // Correct: pass address
    printf("Value: %d\n", data.value);
    return 0;
}
Output
Value: 100
📊

Quick Reference

ConceptSyntax ExampleNotes
Pass by valuevoid func(struct S s);Copies entire structure, safe but slower for large structs
Pass by pointervoid func(struct S *s);Passes address, allows modification, efficient
Call by valuefunc(myStruct);Pass struct variable directly
Call by pointerfunc(&myStruct);Pass address using & operator

Key Takeaways

Pass structures by pointer to efficiently modify original data.
Use pass by value to protect the original structure from changes.
Always use & operator when passing structure pointers to functions.
Large structures should generally be passed by pointer to avoid copying overhead.
Modifications inside functions only affect the original if passed by pointer.