QUESTION 07 · Pointers and Functions
What does int **pp mean?
2 marksAnswer
pp is a pointer to an int * object.
- *pp accesses the int pointer object.
- **pp accesses the int reached through that pointer when both levels are valid.
QUESTION 07 · Pointers and Functions
pp is a pointer to an int * object.
QUESTION 08 · Pointers and Functions
A pointer to a pointer identifies another pointer object. It is useful when a function must update a caller’s pointer value.
int number = 10;
int *p = &number;
int **pp = &p;pp identifies p. Therefore *pp refers to p itself, and **pp reaches number. These levels must be interpreted in order. A declaration of int ** does not automatically create either a pointer target or an int target; all objects must already exist or be allocated appropriately.
redirect receives &p and &second. Its parameter slot identifies main’s pointer p. Assigning *slot = target changes p to identify second. After the call, *p reads 20. The function has changed which integer p points at, rather than assigning 20 into the first integer.
If the function received only a copy of p as int *local, assigning local = &second would change only local. Passing the address of p allows the function to reach and assign main’s actual pointer object. The argument is still passed by value: the copied value is now a pointer to that pointer object.
The slot must identify a live, writable int * object. A null check alone does not prove a non-null pointer is valid. The supplied target must also remain alive while it is used. The sample uses two local ints and one pointer during the same main call, so all lifetimes are suitable. Its fixed assignments use O(1) time and auxiliary space.
/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
void redirect(int **slot, int *target)
{
if (slot != NULL) { *slot = target; }
}
int main(void)
{
int first = 10, second = 20;
int *p = &first;
redirect(&p, &second);
printf("Selected = %d\n", *p);
return 0;
}This example uses values already written in the program.
Selected = 20Related questions in the pointer function arguments category from the supplied papers. The marks below are the printed allocations, including any combined subquestion.
Explain passing pointers to a function, with examples.
Paper P09 · page 2