Learn C
#include "structstypes/struct_params.h"

Passing Structs to Functions in C

#concept

Structs pass like other values: by value (full copy — simple, potentially expensive) or by pointer (no copy, callee can modify). Small structs (a Point, a Range) pass by value readably; large ones travel as const struct * for speed with safety.

Returning structs by value is legitimate and often clear (Point mid(Point a, Point b)), with modern compilers eliding copies. For out-results, prefer a pointer parameter plus a status return over returning oversized values.

#how it works
1

Small: pass by value

double dist(struct Point a, struct Point b) — copies of two-int structs cost nothing and can't alias.

2

Large: const pointer

void show(const struct Big *b) — zero copy, compiler-enforced read-only, the standard big-struct signature.

3

Return small values directly

struct Point mid(...) { ...; return m; } — clear ownership, copies elided in practice.

4

Out-params for the rest

int parse(const char *s, struct Out *o) — status return plus filled struct, C's multi-result pattern.

#example
#include <stdio.h>
 
typedef struct {
int x;
int y;
} Point;
 
Point add(Point a, Point b) {
Point r = {a.x + b.x, a.y + b.y};
return r;
}
 
int main(void) {
Point p = add((Point){1, 2}, (Point){3, 4});
printf("%d,%d\n", p.x, p.y);
return 0;
}
Output
4,6
1add takes two Points by value and returns a Point — copies in, copy out, no aliasing possible.
2Compound literals (Point){1,2} build temporaries inline at the call.
34,6 prints — coordinate-wise addition through pure value semantics.
#use cases
Small value types (points, ranges, complex) passed and returned directly
Large records via const pointers with zero copy overhead
Compound literals for inline temporaries at call sites
Status-plus-struct-out parameters for fallible producers

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C yourself — building beats watching.