No history yet

Pointer Basics

The What and Why of Pointers

Imagine your computer's memory is a street lined with houses. Each house has a unique address. When you declare a variable, like int my_age = 30;, you're essentially building a house at a specific address and putting the number 30 inside it.

A pointer is like a slip of paper where you've written down that house's address. The slip of paper isn't the house itself, and it doesn't hold the number 30. It just holds the location of the house. By looking at the address on the paper, you know exactly where to go to find the number 30.

In C programming, a pointer is a variable that stores the memory address of another variable.

This ability to work directly with memory addresses is what makes C so powerful and efficient. It allows for more complex data structures and gives you fine-grained control over how your program uses memory.

Putting Pointers to Use

To work with pointers, you need to know about two special operators: & and *. They might seem confusing at first because * is used for two different things, but the context makes it clear.

Declare

verb

To define a variable's type and name for the compiler before using it.

First, let's declare a pointer. You do this by specifying the data type it will point to, followed by an asterisk * and the pointer's name.

// This declares a pointer that can store the address of an integer.
int *p_score;

The int part is crucial. It tells the compiler, "The address stored in p_score will point to a location that holds an integer." The asterisk * tells the compiler that p_score is a pointer, not a regular integer variable.

The Address-Of Operator (&)

Now that we have a pointer, we need an address to put in it. The address-of operator, &, gives you the memory address of any variable. Think of it as asking, "Where do you live?"

#include <stdio.h>

int main() {
    int score = 100;
    
    // Use the & operator to get the address of 'score'
    // and store it in the pointer p_score.
    int *p_score = &score;

    printf("Value of score: %d\n", score);
    // Note: %p is the format specifier to print a memory address
    printf("Address of score: %p\n", p_score);

    return 0;
}

In this example, &score gets the memory location of the score variable. We then assign this address to our pointer, p_score. Now, p_score is officially pointing to score.

The Dereference Operator (*)

Here's where the asterisk * reappears, but with a different job. When you place it in front of an already initialized pointer variable, it's called the dereference operator. It means "go to the address stored in this pointer and get the value that's there."

It's like using the address on your slip of paper to go to the house and see what's inside.

#include <stdio.h>

int main() {
    int score = 100;
    int *p_score = &score; // Pointer holds the address of score

    // Use the * operator to get the value AT the address p_score holds
    int value_from_pointer = *p_score;

    printf("Value from original variable: %d\n", score);
    printf("Value from dereferencing pointer: %d\n", value_from_pointer);

    return 0;
}

Notice the difference. When we declare int *p_score, the * tells the compiler it's a pointer. When we use *p_score later, the * tells the program to fetch the value at the pointer's stored address.

You can even use the dereference operator to change the original variable's value.

Modifying the value through the pointer directly changes the original variable because they both refer to the same memory location.

#include <stdio.h>

int main() {
    int score = 100;
    int *p_score = &score;

    printf("Original score: %d\n", score);

    // Dereference the pointer to change the value at that address
    *p_score = 150;

    printf("New score: %d\n", score); // score is now 150!

    return 0;
}

This is just the beginning of what pointers can do. Mastering them is a key step to becoming a proficient C programmer.