topskill.dev
CoursesTracks
All courses/C Programming for BeginnersPointers: Addresses and Dereferencing
Pointers, Addresses, and Lifetime
1 / 9
← C Programming for Beginners
○What a program actually does○The computer and the operating system○From a C file to a running program○Compiled and interpreted programs○Trace One Tiny C Program○Print the Opening Sign✦Module Quiz
○Your First C Program○Variables and Formatted Output○Arithmetic and Integer Division○Formatting and Numeric Conversion○Calculate a Shop Total✦Module Quiz
○Declare and initialize variables○Label the parts bin○Assign a new value○Update the checkout board○Choose int, double, and char○Write a battery test label○Keep local names and constants clear○Count free kit slots✦Module Quiz
○Match printf placeholders and arguments○Print a radio test slip○Build lines with escapes and multiple calls○Print a clock pickup note○Print percent signs and aligned values○Line up a cleaning report○Evaluate expressions before printing○Divide reflectors among trays✦Module Quiz
○Comparisons and True or False○How if and else Choose a Path○Choose a Ticket Message○Select a Fixed Choice with switch○Choose a Service Desk✦Module Quiz
○For Loops: Repeat a Known Number of Times○Add Daily Contributions○While Loops: Repeat While a Condition Holds○Make a Countdown○Run Once with do while○Label a Two-Row Shelf○Label Tool Drawers✦Module Quiz
○Define and Call a Function○Follow a Function Call and Its Return Value○Return a Square○Declare Before a Function Call○Declare and Convert✦Module Quiz
○Arrays and Zero-Based Indexes○Add Array Elements○Count Array Slots Safely○Average Workshop Ratings○Rows and Columns in Memory○Total a Two-Row Grid✦Module Quiz
○C Strings and the Null Terminator○Count a Label○Compare Complete Strings○Verify a Pickup Code○Visit Each Character○Count a Letter in a Label✦Module Quiz
●Pointers: Addresses and Dereferencing○Update Through a Pointer○Pass an Address to a Function○Swap Two Counts○Array Addresses and Offsets○Total with an In-Bounds Pointer○Null, Validity, and Lifetime○Handle an Optional Pointer✦Module Quiz
○Pass an Array and Its Length○Total Readings in a Function○Change Array Elements Through a Parameter○Update Each Stock Count✦Module Quiz
○Define a Supply Record○Record Thread Supplies○Update a Record Member○Update the Paper Stock○Walk an Array of Records○Total the Flyer Stacks○Pass a Record to a Function○Print a Badge Record✦Module Quiz
○One Value in Shared Storage○Record One Ticket Value○A Tag Tells You What Is Stored○Read a Tagged Repair Ticket○Compare Union and Struct Storage○Choose the Right Storage✦Module Quiz
○Read Bits with AND○Check an Equipment Flag○Turn a Flag On with OR○Mark a Kit Ready○Toggle and Clear Individual Flags○Toggle a Warning Flag○Move a Bit into Position○Build a Position Mask✦Module Quiz
○Include the Right Header○Measure a Label with strlen○Guard a Macro Default○Set a Fallback Badge Limit○Include Code Only for Debug Builds○Show an Inventory Diagnostic When Enabled○Parenthesize a Simple Macro○Keep a Macro Calculation Grouped✦Module Quiz
○Name a Status with enum○Confirm an Application Status○Alias a Record Type with typedef○Initialize a Ribbon Record✦Module Quiz
○Where the storage comes from○Request and Release an Array○Store Three Repair Counts○Start Allocated Counters at Zero○Start Station Counters at Zero○Allocate and Release Records○Allocate the parts report✦Module Quiz
○Print a Formatted Label○Print the carving sign○Print Characters with putchar○Build a two-letter mark○Format into a Bounded Buffer○Prepare the shelf label○Write a Line to stdout with fputs○Post the ready notice○Input and Diagnostics in a Local Terminal✦Module Quiz

Pointers: Addresses and Dereferencing

Reach a value through its address

The string comparisons distinguished matching text from matching addresses. Now we will use an address to reach one existing value. This gives us a way to update that value without making a separate copy.

An object is a region of storage holding a value, such as the integer variable count. Its address identifies that object in memory. A pointer holds an address value; the object it points to is sometimes called its pointee.

After declaring count, int *location = &count; gives location the address of count. The * in the declaration makes this an int pointer; later, *location means the integer at that address. Reading or assigning through it reaches count itself, not a separate copy. Always initialize a pointer before dereferencing it; an uninitialized pointer does not identify an object you can use.

The first print reads through the pointer. The assignment through *location changes count, so the second print sees the new value. The address stored in location stays the same:

c
#include <stdio.h>

int main(void) {
    int count = 3;
    int *location = &count;
    printf("Before: %d\n", *location);
    *location = 5;
    printf("After: %d\n", count);
    return 0;
}

There are two objects: count stores an integer, and location stores a pointer value. The expressions count and *location access the same integer object. Updating *location changes that integer; it does not replace the address stored in location.

Address, pointee, and lifetime

The diagram shows &count rather than a numeric address because the address can differ between runs. At each dereference, location must still point to a live int. Making up a numeric address does not make a usable pointer. All addresses in these diagrams are symbolic labels, not observed numeric addresses. If you need to print an object pointer in C, use %p with a (void *) conversion, not %d; the exact printed address is implementation-dependent, so these exercises check the pointed-to values instead.