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.

p: follow the addressesp stores the address of value. Writing *p changes value; assigning p itself would change only the local pointer parameter. pparameter: int * value = 42caller integer
p stores the address of value. Writing *p changes value; assigning p itself would change only the local pointer parameter.

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 **.

out: follow the addressesAfter allocation, out points to array; array points to integer data. Dereference once to reach the caller pointer, and again to reach an integer. outparameter: int ** arraycaller pointer: int * 12integer data
After allocation, out points to array; array points to integer data. Dereference once to reach the caller pointer, and again to reach an integer.
ExpressionWhat it names
outThe address of the caller’s pointer variable.
*outThe caller’s pointer variable itself.
**outThe 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.

A complete example: produce a pointer and a length
C codeWhat this does
#include <stdio.h>
#include <stdlib.h>
Declare printf, malloc, and free.
void makeNumber(int **out, int *length) {
    *out = NULL;
    *length = 0;
Start with an empty result, including on allocation failure.
    int *block = malloc(sizeof(int));
    if (block == NULL) return;
Request storage for one integer. Stop if allocation fails.
    *block = 12;
    *out = block; // update the caller pointer
    *length = 1; // update the caller integer
}
Initialize the data, then store both outputs through their addresses.
int main(void) {
    int *number = NULL;
    int count = 0;
    makeNumber(&number, &count);
Pass the address of each variable that the function must update.
    if (number == NULL) return 1;
    printf("%d %d\n", count, number[0]);
    free(number);
    return 0;
}
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 memcpy for the name bytes, including the null terminator, within the name array’s capacity.
  • In the caller, array[i].field selects 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 objectPassParameter type
int value&valueint *
int *array&arrayint **
struct student *array&arraystruct student **

Check yourself

  1. Why does setLocal(value) leave value unchanged?
  2. If p points to value, which assignment updates value: p = ... or *p = ...?
  3. If array is an int *, what is the type of &array?
  4. In makeNumber, what are count and number after allocation failure? Who frees the allocation on success?
Answers
  1. Its parameter is a separate copy of the integer.
  2. *p = ... writes to the integer at the stored address.
  3. int **: the address of an int * variable.
  4. count is 0 and number is NULL. On success, the caller frees number after its last use.