No history yet

Introduction to Pointers

The Address Book of Memory

Think of your computer's memory as a vast street of houses. Each house has a unique address. When you declare a variable, like int my_age = 30;, you're essentially putting the number 30 inside a house and labeling that house my_age.

A pointer is different. It's a special kind of variable that doesn't hold a value like 30. Instead, it holds the address of another house. It's like having a piece of paper with a friend's address written on it. The paper itself isn't your friend, but it tells you exactly where to find them.

In C, pointers are crucial. They let us work directly with memory locations, which is key for managing memory efficiently, building complex data structures like linked lists, and passing large amounts of data to functions without copying everything.

In C, pointers are the variables that can store the address of other variables, functions, structures, and even other pointers.

Declaring and Using Pointers

To declare a pointer, you use an asterisk * between the data type and the variable name. This tells the compiler that the variable will store an address of that data type.

int *p_age; // Declares a pointer named p_age that can hold the address of an integer.

To get the memory address of a variable, you use the "address-of" operator, &. To assign this address to your pointer, you do it like any other variable assignment.

int my_age = 30;
int *p_age = &my_age; // p_age now stores the memory address of my_age.

Now, how do you see the value that the pointer is pointing to? You dereference it using the asterisk * again. This tells the program to go to the address stored in the pointer and retrieve the value at that location.

#include <stdio.h>

int main() {
    int my_age = 30;
    int *p_age = &my_age; // Store the address of my_age in p_age

    // Print the address stored in the pointer
    printf("Address stored in p_age: %p\n", p_age);

    // Dereference the pointer to get the value it points to
    printf("Value at that address: %d\n", *p_age);

    return 0;
}

Pointer Arithmetic

You can perform basic arithmetic operations on pointers. This is especially useful when working with arrays, because array elements are stored sequentially in memory.

When you increment a pointer, it doesn't just add 1 to the memory address. Instead, it moves forward by the size of the data type it points to. For an int pointer, incrementing it moves the address forward by sizeof(int) bytes (usually 4 bytes), right to the location of the next integer in memory.

If p is an int pointer and its address is 1000, p++ will change its address to 1004, not 1001.

This makes it easy to loop through an array. A pointer to the first element can be incremented to access each subsequent element in order.

#include <stdio.h>

int main() {
    int numbers[] = {10, 20, 30, 40};
    int *p_nums = numbers; // An array name acts like a pointer to the first element

    printf("First element: %d\n", *p_nums);

    // Move the pointer to the next element
    p_nums++;
    printf("Second element: %d\n", *p_nums);

    // Move it again
    p_nums++;
    printf("Third element: %d\n", *p_nums);

    return 0;
}

Understanding this behavior is the first step to using pointers to efficiently navigate blocks of memory.

Ready to check your understanding?

Quiz Questions 1/4

What does a pointer variable store?

Quiz Questions 2/4

Which operator is used to get the memory address of a variable?

Pointers can seem tricky at first, but they are a powerful tool. Mastering them unlocks a deeper level of control and efficiency in your C programs.