C Reference ยท Functions and Scope
Pointer parameters in C
Reference for pointer parameters in C: change the caller's variables, return several values through out-parameters, use const pointers and check for NULL.
C passes arguments by value, so a function cannot change the caller's variable directly. A pointer parameter solves this: the caller passes the address of the variable, and the function reads or writes through that address with the * operator. The pointer itself is still a copy, but both copies point at the same object.
Pointer parameters are used for three jobs: modifying a caller's object (an in/out parameter), returning extra results (an out-parameter), and avoiding a copy of a large object (usually with const).
void swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; } /* call: swap(&x, &y); */
Try it
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.
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.
Syntax
void f(T *p); /* may read and write the pointed-to T */ void f(const T *p); /* may only read */ void f(T *const p); /* the parameter itself cannot be reassigned */ void f(T **pp); /* can change which T the caller's pointer refers to */ f(&object); /* call: pass the address */
| Name | Description |
|---|---|
T *p |
A copy of the caller's address. *p is the caller's object. |
const T *p |
The data may not be modified through p. Callers can pass both const and non-const objects. |
T **pp |
Used when the function must change the caller's pointer (allocate, advance a cursor). |
Errors / undefined behavior: dereferencing a null, uninitialized or dangling pointer is undefined behavior. Passing a pointer to an object of the wrong type (through a cast) can break aliasing rules. Writing through a const-qualified original object is undefined even after casting const away.
Usage notes
- Check for
NULLwhen the API allows it, and document whetherNULLis permitted. A parameter that must not be null can be documented with[[gnu::nonnull]]or just a comment; C has no reference type. - Prefer
const T *for inputs. It tells readers (and the compiler) the function will not modify the data, and it accepts string literals without warnings. - Out-parameters and errors. Return a status code and deliver values through pointers, as in
divide()above. Write to the outputs only on success, so callers do not see half-updated values. - Reassigning the parameter (
p = other;) changes only the local copy. To change the caller's pointer, takeT **. - Aliasing.
swap(&x, &x)with the three-step swap is harmless, but XOR-swap tricks break. Pointers that may refer to the same object also limit optimization;restrictpromises they do not. - Never return the address of a local or a parameter: it dies when the function returns. See return values.
- Pointers to a struct are the normal way to pass big records:
void print_user(const struct user *u), withu->nameto access fields. - Arrays arrive as pointers automatically; see array parameters.