C Pointers, Addresses and Dereferencing
C Pointers, Addresses and Dereferencing
A helper function should correct a mark stored by its caller. It needs access to that stored value, rather than a separate copy of the old mark.
A pointer is a value that identifies another value’s location. In C, a stored value such as marks is called an object. The & operator obtains its address; the unary * operator accesses the object reached through a valid pointer. A null pointer points to no object and must not be used for that access.
Explain
Trace a copied pointer to the caller-owned object it designates.
Trace
Add a five-mark bonus through a pointer and predict the caller’s value.
Apply
Recognize a pointer that must not be dereferenced.
Pointers: follow the data
Start
A helper function should correct a mark stored by its caller. It needs access to that object, not merely a copy of the old mark.
Operation
A pointer value designates an object or function, or is null. & obtains an object’s address; unary * accesses the object designated by a valid pointer. The pointer’s type describes the pointed-to type.
Result
Corrected: 45
Understand the operation
int *target declares a pointer to int. &marks supplies the address of an existing int object. When correct receives that address, its pointer parameter is a copy, but dereferencing the copy reaches the same caller-owned int.
A school locker number is a limited analogy: the pointer identifies where access is permitted, but it is not a portable integer offset to arbitrary storage. C type, lifetime and bounds rules still govern every access.
Read the complete C program
This complete C17 program uses a small school example. Read declarations before the statements that use their values. Each printf format describes how the corresponding value is written to standard output.
C17 · complete program
#include <stdio.h>
static void correct(int *mark) {
*mark = 45;
}
int main(void) {
int marks = 40;
correct(&marks);
printf("Corrected: %d\n", marks);
return 0;
}
Expected output
Corrected: 45
Run in C compiler →Follow the values through the code
C17 · code used in this explanation
#include <stdio.h>
static void correct(int *mark) {
*mark = 45;
}
int main(void) {
int marks = 40;
correct(&marks);
printf("Corrected: %d\n", marks);
return 0;
}
main creates a live int object containing 40 and obtains its address.
correct stores 45 through *mark. After returning, main reads its own object and observes that changed value.
Follow the worked calculation
Read the starting values
main creates a live int object containing 40 and obtains its address.
Perform the operation
correct stores 45 through *mark. After returning, main reads its own object and observes that changed value.
Check the observable result
Corrected: 45
Corrected: 45
Try a small variation
C17 · code used in this explanation
#include <stdio.h>
static void correct(int *mark) {
*mark = 45;
}
int main(void) {
int marks = 40;
correct(&marks);
printf("Corrected: %d\n", marks);
return 0;
}
In main, declare a pointer to marks and use it to add a five-mark bonus. Predict whether reading marks afterward shows 40 or 45.
Worked answer
The pointer designates marks throughout this scope. Reading and updating *target changes that existing object; it does not allocate or create a second mark.
C17 · complete program
#include <stdio.h>
int main(void) {
int marks = 40;
int *target = &marks;
*target += 5;
printf("Corrected: %d\n", marks);
return 0;
}
Expected output
Corrected: 45
Run in C compiler →Recognize the important boundaries
Do not dereference an uninitialized, null, freed or otherwise invalid pointer. Merely looking like an address does not make a pointer valid.
C still passes the pointer argument by value. The traditional phrase call by reference describes the effect informally, not a separate C parameter-passing mechanism.
Check your understanding
Choose the answer, then compare the reasoning for every option.
Use the idea independently
A pointer value designates an object or function, or is null. & obtains an object’s address; unary * accesses the object designated by a valid pointer. The pointer’s type describes the pointed-to type.
Continue with pointer bounds and array parameters.
Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.
Reaching the recap and completing the quizzes are separate milestones.