No history yet

Introduction to Pointers

What Are Pointers?

Think of your computer's memory as a vast street of houses. Each house has a unique address. A variable in your program is like one of those houses, storing some information, and its memory address is like the house's street address.

A pointer is a special kind of variable that doesn't store data like a number or a character. Instead, it stores a memory address. It's like a piece of paper where you've written down a friend's address. The paper itself isn't your friend's house, but it tells you exactly where to find it. Pointers give us a way to work directly with memory locations, which is a powerful and efficient feature of C.

This direct access allows for creating dynamic data structures, managing memory manually, and writing faster code.

Working with Pointers

To use pointers, you need to know three key operators: * (for declaring and dereferencing) and & (for getting an address).

First, you declare a pointer using an asterisk (*) between the data type and the variable name. This tells the compiler that the variable will hold the address of that data type.

int *p; // p is a pointer to an integer
char *name; // name is a pointer to a character

An uninitialized pointer points to a random, invalid memory location, which can cause crashes. It's good practice to initialize pointers to NULL (a special value meaning it points to nothing) or to the address of an existing variable.

To get the memory address of a variable, you use the address-of operator (&). To access the value stored at the address a pointer is holding, you use the dereference operator (*).

Think of it this way: & gives you the address, and * follows that address to get the value.

Here's how they work together:

#include <stdio.h>

int main() {
    int score = 95;
    int *p_score = NULL; // Declare and initialize a pointer

    p_score = &score; // Assign the address of 'score' to the pointer

    // Print the value of score directly
    printf("Value of score: %d\n", score);

    // Print the address of score
    printf("Address of score: %p\n", &score);

    // Print the address stored in the pointer
    printf("Value of p_score (the address it holds): %p\n", p_score);

    // Access the value at the address using the pointer
    printf("Value pointed to by p_score: %d\n", *p_score);

    return 0;
}

In this example, p_score holds the memory address of score. When we dereference p_score with *p_score, we're essentially saying, "go to the address stored in p_score and give me the value you find there." That value is 95.

Pointers in Action

Pointers become truly useful when we start manipulating them. This includes performing arithmetic and using them with arrays and functions.

Pointer Arithmetic

You can perform arithmetic operations like addition and subtraction on pointers. However, it works differently than with regular numbers. When you add 1 to an integer pointer, you're not just adding 1 to the memory address. Instead, the compiler adds the size of the data type the pointer points to.

For an int that takes 4 bytes, p_score++ will advance the memory address stored in p_score by 4 bytes, moving it to where the next integer would be in memory. This is crucial for navigating sequences of data, like arrays.

Pointers and Arrays

In C, an array's name is essentially a constant pointer to its first element. This means you can use pointer notation to access array elements. The expressions grades[i] and *(grades + i) are equivalent.

int grades[] = {88, 92, 75};
int *p_grades = grades; // No '&' needed for arrays

// Access the second element (92)
printf("%d\n", *(p_grades + 1));

Using pointers to walk through an array is a common and efficient technique in C programming.

Pointers and Functions

When you pass a variable to a function, C typically makes a copy of it. Any changes the function makes are to the copy, not the original. But what if you want a function to modify your original variable? You pass a pointer to it.

By passing the address of a variable, the function can dereference the pointer and change the value at that original memory location.

#include <stdio.h>

// This function takes a pointer to an integer
void add_five(int *num) {
    *num = *num + 5; // Modify the value at the original address
}

int main() {
    int my_value = 10;
    printf("Original value: %d\n", my_value);

    add_five(&my_value); // Pass the ADDRESS of my_value

    printf("New value: %d\n", my_value);

    return 0;
}

The function add_five receives the address of my_value. It then accesses the original variable through the pointer and modifies it directly. This is a fundamental concept for writing functions that need to return multiple values or modify existing data structures efficiently.

Quiz Questions 1/6

What is a pointer in C programming?

Quiz Questions 2/6

Which operator is used to retrieve the value stored at the memory address a pointer is pointing to?

These are the building blocks of using pointers in C. Mastering them is key to unlocking more advanced programming techniques.