C Structures and Functions

Intermediate
6 min read
Updated: Nov 2026
1 example

What Is Structures and Functions?

You can pass a struct by value (the function gets a copy), by pointer (cheap and modifiable), or return a struct by value. Small structs are fine by value; for large ones pass const struct T * for read-only access and struct T * to modify.

Remember
struct Vec add(struct Vec a, struct Vec b);   /* by value, returns struct */
void scale(struct Vec *v, int k);              /* modifies caller */
double length(const struct Vec *v);            /* read-only       */

Cheat Sheet

Keep these facts handy while you work with Structures and Functions in C.

TopicDetail
By valueCopy in, copy out
By pointerModify or avoid copying
ReturnStruct returned by value is allowed
constconst T * for inputs

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Unions.

Example 1 — Add and scale vectors

One function returns a new struct; another modifies in place.

C
#include <stdio.h>

struct Vec { int x, y; };

static struct Vec add(struct Vec a, struct Vec b) {
    struct Vec r = {a.x + b.x, a.y + b.y};
    return r;
}

static void scale(struct Vec *v, int k) {
    v->x *= k;
    v->y *= k;
}

int main(void) {
    struct Vec a = {1, 2}, b = {3, 4};
    struct Vec s = add(a, b);
    printf("sum = (%d, %d)\n", s.x, s.y);
    scale(&s, 2);
    printf("scaled = (%d, %d)\n", s.x, s.y);
    return 0;
}

How It Works

  1. add works on copies and returns a new struct Vec.
  2. scale receives &s, so it changes s directly.
  3. 1+3 = 4, 2+4 = 6, then both double.

Edge Cases & Pitfalls

Check these before relying on Structures and Functions in real programs.

big

Passing huge structs by value

Each call copies every byte. Pass a pointer for large structs.

ptr

Returning pointer to local struct

A pointer to a local struct dangles after return. Return the struct by value or use the heap.

Key Takeaways

  • Small: by value is fine.
  • Large or modified: by pointer.
  • Read-only: const T *.

One line: You can pass a struct by value (the function gets a copy), by pointer (cheap and modifiable), or return a struct by value.

Frequently Asked Questions

Yes. The struct is copied to the caller, which is safe even though it was a local variable.
Pass by pointer for large structs or when the function must modify them; by value is fine for small, read-only structs.
Continue with "C Unions" next. Store different types in the same memory, one at a time. The C Structures, Unions and Enums hub lists every topic in order.

Next: Unions

Store different types in the same memory, one at a time.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful