C Programming · Module 1
C Functions, Pointers and Memory
A C function receives values. A pointer is also a value, but it identifies an object that the function can access. Understanding that distinction explains why a swap sometimes changes the caller’s variables and sometimes changes only local copies.
1. Follow the values through a call
Consider int twice(int n) { return n * 2; }. In int result = twice(7);, the argument is 7 and the parameter is the local variable n. The function returns 14. Changing n inside the function does not change a caller’s separate integer.
A declaration such as int twice(int n); tells the compiler the parameter and return types before a call. The definition also contains the executable body. Put a declaration in a header when several source files need the same interface, and provide one definition in a source file.
2. Update the original object through a pointer
void swap(int *a, int *b) {
int temporary = *a;
*a = *b;
*b = temporary;
}
/* With int x = 4, y = 9; call swap(&x, &y). */
The call passes the addresses of x and y. The parameters hold copies of those addresses. Dereferencing a accesses x, so the assignment changes the caller’s object. After the call, x is 9 and y is 4. This is often described informally as “call by reference”, but C’s parameter-passing mechanism remains pass by value.
The function requires valid pointers to writable integers. Its temporary-variable version also works when both pointers identify the same integer. Arithmetic or XOR swap tricks need extra care and are usually less clear. Addition-based swaps can cause undefined behavior through signed overflow; they are poor general replacements for a temporary variable.
3. Decide who owns allocated memory
Automatic local variables normally live until their block exits. Dynamically allocated storage remains allocated until explicitly released. Returning the address of an ordinary local variable leaves the caller with an invalid pointer after return. Returning a pointer to an allocated object can be valid if the interface clearly says who must free it.
#include <stdlib.h>
int *make_pair(int first, int second) {
int *values = malloc(2 * sizeof *values);
if (values == NULL) return NULL;
values[0] = first;
values[1] = second;
return values; /* The caller must free the returned allocation. */
}
The caller checks the result before using it, then calls free(values) once when finished. Do not read or write through the pointer after freeing it. Setting one pointer to NULL does not repair other aliases to the released object.
4. Resize without losing the old allocation
For a nonzero new size, assign realloc to a temporary pointer. If allocation fails, the original allocation is still available. Overwriting the only original pointer directly with a failed result loses access to that memory. Check element counts and multiplication for size overflow before allocating user-controlled amounts.
5. Check your understanding
- If
void change(int n) { n = 99; } is called with x = 4, x remains 4: only the local parameter changed. - After
swap(&x, &x) with the temporary-variable function, x keeps its original value. - A successful allocation does not initialize integers unless the chosen allocation operation does so. Write values before reading them.
Further reference: WG14 C11 draft, sections 6.5.2.2 (function calls) and 7.22.3 (memory management). These are language rules; compiler diagnostics and sanitizers provide additional checks during development.
|