Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 135 ยท Functions

Parameter Passing: Call by Value and by Pointer

See exactly what C copies when you call a function, how pointers simulate call by reference, and how arrays, structs and pointer parameters behave in practice.

Languages such as C++, Pascal and C# let you mark a parameter as a reference, so the function works directly on the caller's variable. C has no such feature. It has one way to pass arguments, call by value, and one trick on top of it: pass a pointer. Almost every confusing C function signature (void swap(int *a, int *b), int fill(char buf[], size_t n), void grow(int **list)) is a variation on that single idea.

Parameters and Return Values introduced the rule. This lesson goes deeper: it proves what gets copied, compares the cost of passing different kinds of data, and works through the cases that trip people up, such as passing structs, passing arrays and changing a pointer inside a function.

What exactly is copied?

When you call f(x), the compiler creates a brand-new local variable for the parameter and initializes it with the value of the argument. The two variables have different addresses. You can check that directly, without printing any machine-specific numbers, by comparing the address of the argument with the address of the parameter:

C Two variables, two addresses

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The function sees the value 7, confirms that its param lives at a different address than arg, and changes only its own copy. This is why a function can never alter arg just by receiving it.

Simulating call by reference with pointers

If a function needs to change the caller's variable, give it the variable's address. The address itself is still passed by value (the function gets a copy of the pointer), but both copies hold the same address, so writing through either one modifies the same object.

C swap with pointers, and a min/max out-parameter pair

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

In the call swap(&x, &y) the caller says "you may modify these two variables", and the signature says so too: int *a is a promise that the function may write through a. When a function only reads through a pointer, add const (as min_max does for values), so the compiler rejects accidental writes and readers know the data is safe. See const and Pointers for the details.

Passing a pointer is still call by value. Some textbooks call it "call by reference", but in C the language only ever copies; pointers are how you build the reference.

Passing structs: copies can be expensive

A struct is passed by value like everything else: the whole object is copied into the parameter. For a small struct such as a point that is fine, but a struct holding a large array is copied byte by byte on every call.

C Struct by value versus by pointer

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The record is 204 bytes on a typical system, the pointer only 8. A common rule of thumb: pass small structs (up to a couple of machine words) by value, and pass larger ones by pointer, using const struct record * when the function only reads them. Passing by value has one real benefit: the caller's data is guaranteed untouched. Remember that a copy is shallow: if the struct contains a pointer, the copy points at the same memory as the original. Structures and Pointers covers this.

Arrays are different, but not really

You cannot pass an array by value in C. When an array name appears as an argument, it decays to a pointer to its first element, and that pointer is what gets copied. So the function can modify the caller's elements, and it does not know the array's length unless you pass it too.

C Array parameters are pointers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Writing int values[] in the parameter list is only a hint to the reader; the compiler treats it as int *values. Inside the function sizeof values is the pointer size, which is why a length parameter is essential. To get true by-value behavior for an array, wrap it in a struct, because structs are copied whole.

Changing a pointer needs a pointer to a pointer

Suppose a function should allocate memory and give it to the caller. If the parameter is a plain char *, the function would only change its local copy of the pointer. To change the caller's pointer you pass the pointer's address, giving a parameter of type char **.

C Why allocate-and-return needs char **

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

broken_copy is a classic bug, and it also leaks the memory it allocated, because the only pointer to it vanished when the function returned. The fix has the same shape as swap: to modify a variable of type T, pass a T *. Here T is char *, so the parameter is char **. Returning the pointer instead (char *make_copy(const char *)) is often even simpler; use the out-parameter form when the return value is needed for a status code. Pointers to Pointers has more patterns.

Choosing how to pass data

What you pass What is copied Can the function change the caller's data?
int, double, char the value no
int *, struct s * the address yes, through the pointer
const int *, const struct s * the address no (the compiler enforces it)
struct s the whole struct no, only its copy
an array name the address of element 0 yes, the elements
T ** the address of a pointer yes, including the pointer itself

A few habits follow from the table. Pass small values by value. Pass big or mutable things by pointer. Mark read-only pointer parameters const. Pass lengths alongside array pointers. And if a function writes through a pointer, say so in its name or documentation (fill_buffer, swap), so callers are not surprised.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. After void inc(int n) { n++; } and the calls int a = 1; inc(a);, what is a?

    C passes the value of a, so inc increments its own copy. The caller's a stays 1.
  2. 2. Which signature lets a function change an int variable of the caller?

    Only a non-const pointer to the variable lets the function write to it through *n. A const pointer forbids writes, and a plain int is a copy.
  3. 3. A function must make the caller's char * point to newly allocated memory. What should the parameter type be?

    To modify a variable of type char *, you pass its address, which has type char **. A plain char * only changes a local copy.
  4. 4. Why is sizeof values the size of a pointer inside void f(int values[])?

    An array argument decays to a pointer to its first element, and an array parameter declaration is adjusted to a pointer type. The length is lost, so pass it separately.

Summary

  • C passes every argument by value: the function always works on a copy, and the copy has its own address.
  • Passing a pointer copies the address, so the function can modify the caller's object through it. That is how you simulate call by reference.
  • Structs are copied whole. Pass large ones by pointer, with const when they are only read.
  • Arrays decay to pointers, so the callee changes the caller's elements and loses the length.
  • To change a pointer variable itself (for example to allocate for the caller), pass a pointer to that pointer.

Next: Declarations, Prototypes and Headers, where you'll learn how to call a function before it's defined.