Show how a function can change caller variables using pointer arguments.
5 marks
Answer in brief
A function can modify a caller’s object when it receives a valid pointer to that writable object and assigns through the pointer.
Required notation
int *p declares a parameter that can hold the address of an int.
&x obtains the address of x.
*p accesses the int identified by p. An assignment such as *p = 20 changes that object.
Swap algorithm
Save the value of *a in a temporary integer.
Copy *b into *a.
Copy the temporary value into *b.
Example trace
main starts with x = 4 and y = 9. swap(&x, &y) passes their addresses. The parameter a identifies x and b identifies y. temporary receives 4, *a becomes 9 and *b becomes 4. After the call, the original objects in main contain the swapped values.
Important distinction
This is often called call by address in introductory courses. C itself still passes arguments by value: the pointer values are copied. Assigning a different address to the local parameter a would not change a caller pointer variable. Assigning to *a changes the object reached through the copied pointer.
Validity and complexity
Both pointers must identify suitable live, writable int objects before they are dereferenced. The example always passes addresses of local variables that remain alive during the call. There are three fixed assignments and one temporary integer, so the swap takes O(1) time and O(1) auxiliary space. Full pointer details belong to Unit V.
/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
void swap(int *a, int *b)
{
int temporary = *a;
*a = *b;
*b = temporary;
}
int main(void)
{
int x = 4, y = 9;
swap(&x, &y);
printf("x = %d, y = %d\n", x, y);
return 0;
}
Sample input and output
This example uses values already written in the program.
Output
x = 9, y = 4
Why assigning to the parameter is different from assigning through it
Suppose main contains int x = 4; and int *p = &x;. A function called with p receives a copy of the address stored in p. Call that local parameter q. Inside the function, suppose another local integer is declared as int other = 9;.
↔ Scroll horizontally on a small screen
A copied pointer and the object it identifies
Statement inside the function
Effect inside the function
Effect visible to main
*q = 20;
Write to the int identified by q
x becomes 20
q = &other;
Make q identify other instead
p still identifies x; x is unchanged by this assignment
The first statement changes an integer. The second changes only the function’s local pointer variable. A pointer is a value that can be copied, just like an integer; copying it does not create a new copy of the pointed-to object.
Trace the swap in the existing program
temporary = *a saves 4 before x is overwritten.
*a = *b places 9 in x. At this point x and y both contain 9.
*b = temporary places the saved 4 in y.
Without the temporary value, the original 4 would be lost after the second step. Calling swap(&x, &x) is also valid for this implementation: each assignment uses the same live integer and its value stays unchanged. Passing a null pointer is invalid because the function dereferences both parameters.
Related exam questions
Related questions in the function arguments category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.
March/April 2023 · Q1(g) · 2 marks
Define formal arguments.
Paper P02 · page 1
March/April 2023 · Q8(b) · 5 marks
Write a C program using functions to swap two numbers using global variables and the call-by-reference concept.