ProgramGuru

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.

  1. Explain

    Trace a copied pointer to the caller-owned object it designates.

  2. Trace

    Add a five-mark bonus through a pointer and predict the caller’s value.

  3. Apply

    Recognize a pointer that must not be dereferenced.

Pointers: follow the data

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

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

  3. Result

    Corrected: 45

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

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

  1. Read the starting values

    main creates a live int object containing 40 and obtains its address.

  2. Perform the operation

    correct stores 45 through *mark. After returning, main reads its own object and observes that changed value.

  3. Check the observable result

    Corrected: 45

    Corrected: 45
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

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.

Why does correct(&marks) change marks?
Choose one answer. You can try again.

Answer and explanation

The copied address allows *mark to access the caller’s object.

Correct. The integer object remains in main; the pointer supplies access to it.

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.

    Next lesson: C Pointer Arithmetic and Array Parameters →