CSSE 132 · Worksheet 24 · Review draft
Passing Pointers for Output: Changing the Caller’s Variables
To change a caller’s variable, give the function that variable’s address.
Why this matters
Sometimes a function needs to produce both an array and its length. Pointer parameters let it write those results into the caller’s variables. Worksheet 24 builds this skill in three steps: update integers, update an array pointer, then work with structures.
1. An ordinary parameter receives a copy
void setLocal(int n) {
n = 42; // changes only the parameter
}
// In main:
int value = 7;
setLocal(value); // value is still 7
The caller’s value and the function’s n are different objects. The call copies 7 into n. Assigning 42 to that copy does not change value.
2. A pointer lets the function reach the caller’s object
void setValue(int *p) {
*p = 42; // writes to the caller
}
// In main:
int value = 7;
setValue(&value); // value is now 42
&value means “the address of value.” The parameter p receives a copy of that address. *p names the integer at that address, so assigning to it changes the caller’s integer.
C still passes by value. Passing an address does not change that rule. The copied address lets the function reach an existing caller object.
In Worksheet 24’s swap problem, identify which parameters receive addresses and which receive integer values. A temporary preserves a value before it is overwritten. The assignment to c affects only a local copy.
3. What if the caller’s variable is a pointer?
Suppose the caller declares int *array = NULL;. To replace the address stored in array, a function needs &array. Because array has type int *, its address has type int **.
| Expression | What it names |
|---|---|
out | The address of the caller’s pointer variable. |
*out | The caller’s pointer variable itself. |
**out | The first integer it points to, once valid storage exists. |
An int ** is not automatically a two-dimensional array. Here it points to one pointer variable. Writing out = ... changes the local parameter; writing *out = ... updates the caller’s pointer.
4. Two outputs: a heap pointer and a length
This smaller example produces one integer rather than solving the worksheet’s array exercise. Assume both output arguments point to valid, distinct caller variables, and the caller pointer starts as NULL.
| C code | What this does |
|---|---|
| Declare printf, malloc, and free. |
| Start with an empty result, including on allocation failure. |
| Request storage for one integer. Stop if allocation fails. |
| Initialize the data, then store both outputs through their addresses. |
| Pass the address of each variable that the function must update. |
| On success, print 1 12. The caller releases the heap block after its last use. |
The local pointer block disappears when the function returns, but its heap allocation survives. number keeps the address. Do not replace an existing owned allocation without first arranging its cleanup.
Worksheet 24’s createArray uses this same two-output pattern with several elements. Inside that function, (*arr)[i] selects element i through the caller’s pointer. Keep the parentheses: *arr[i] means *(arr[i]) and indexes the pointer parameter instead.
5. The same idea works for structures
If the caller’s pointer has type struct student *, its address has type struct student **. The output-pointer pattern stays the same; the allocated element type changes.
- Reserve
len * sizeof(struct student)bytes for len structures. - Initialize each element’s fields before reading them.
- Use
memcpyfor the name bytes, including the null terminator, within the name array’s capacity. - In the caller,
array[i].fieldselects a field of one structure.
Notice that Problem 3 receives len by value: it uses a length the caller already knows. Problem 2 instead sends a new length back through a pointer.
Worksheet note: Problem 3’s loop uses an undefined my_len; its intended bound is len. Allocation needs <stdlib.h>, and memcpy needs <string.h>. Complete programs should use int main(void), check allocations, and free their heap storage.
Common mistakes
- Passing the current value: pass the variable’s address when the function must update that variable.
- Assigning the parameter: changing a local pointer does not replace the caller’s pointer. Write through it.
- Following an invalid pointer: an output pointer must point to an existing caller variable; a NULL output argument cannot be dereferenced.
Quick reference
| Caller object | Pass | Parameter type |
|---|---|---|
int value | &value | int * |
int *array | &array | int ** |
struct student *array | &array | struct student ** |
Check yourself
- Why does setLocal(value) leave value unchanged?
- If p points to value, which assignment updates value: p = ... or *p = ...?
- If array is an int *, what is the type of &array?
- In makeNumber, what are count and number after allocation failure? Who frees the allocation on success?
Answers
- Its parameter is a separate copy of the integer.
- *p = ... writes to the integer at the stored address.
- int **: the address of an int * variable.
- count is 0 and number is NULL. On success, the caller frees number after its last use.