Quiz Entry - updated: 2026.07.10
How do you pass a struct to a function efficiently?
Pass a pointer (struct big *) instead of the struct by value, so the function gets an 8-byte address instead of copying possibly thousands of bytes — add const if it won't modify it.
struct big_data {
int values[1000];
char name[256];
};
// BAD - copies entire struct (4000+ bytes!)
void process_copy(struct big_data data) {
// Only modifies local copy
data.values[0] = 100;
}
// GOOD - passes 8-byte pointer
void process_ptr(struct big_data *data) {
// Modifies original
data->values[0] = 100;
}
// GOOD - const pointer if you won't modify
void print_data(const struct big_data *data) {
printf("%s\n", data->name);
// data->values[0] = 5; // ERROR - can't modify const
}
When to pass by value:
- Small structs (≤ 16 bytes typically)
- When you need a local copy anyway
- When you want to guarantee no side effects
When to pass by pointer:
- Large structs
- When you need to modify the original
- For consistency in an API