No history yet

Introduction to Pointers

Memory and Addresses

Imagine your computer's memory is a long street lined with houses. Each house has a unique address. When you create a variable, like int my_age = 30;, you're essentially building a new house, putting the number 30 inside, and the computer assigns it an address so it can find it later.

A pointer is simply a special type of variable that doesn't store a value like 30. Instead, it stores the address of another variable.

Think of a pointer as a slip of paper where you've written down the address of a friend's house. You don't have your friend with you, but you have the information needed to find them. This ability to work with memory addresses directly is one of the most powerful features of C.

Working with Pointers

To create a pointer, you declare it with an asterisk * between the data type and the variable name. The data type tells the computer what kind of variable the pointer will point to. This is important because memory is handled differently for integers, characters, and other types.

// Declare an integer variable
int score = 95;

// Declare a pointer that can point to an integer
int *score_ptr;

// Initialize the pointer with the address of 'score'
// The '&' is the "address-of" operator
score_ptr = &score;

Now, score_ptr holds the memory address of score. But how do we see the value at that address? We use the asterisk * again, but this time in a different way. It's called the dereference operator. Placing * in front of a pointer's name accesses the value it points to.

// Access the value using the pointer
printf("The score is: %d\n", *score_ptr); // Prints: The score is: 95

// You can also change the original variable's value through the pointer
*score_ptr = 100;
printf("The new score is: %d\n", score); // Prints: The new score is: 100

Pointer Arithmetic

Pointers get really useful when you work with data stored sequentially in memory, like arrays. You can perform basic arithmetic on pointers to move between memory locations.

When you add 1 to a pointer, it doesn't just add 1 to the raw memory address. Instead, it moves the pointer forward by the size of one element of its data type. For an int pointer, ptr + 1 moves to the address of the next integer. If an integer takes up 4 bytes, the address increases by 4.

int numbers[] = {10, 20, 30, 40};
int *ptr = numbers; // An array name acts as a pointer to the first element

// Point to the first element (numbers[0])
printf("Value at ptr: %d\n", *ptr); // Prints: 10

// Move to the next element
ptr++; // This is pointer arithmetic

// Point to the second element (numbers[1])
printf("Value at ptr after increment: %d\n", *ptr); // Prints: 20

// Point to the third element (numbers[2])
printf("Value at ptr + 1: %d\n", *(ptr + 1)); // Prints: 30

This is why specifying the pointer's type is crucial. The compiler needs to know whether ptr++ should jump 4 bytes for an int or just 1 byte for a char.

Quiz Questions 1/5

In C programming, what does a pointer variable primarily store?

Quiz Questions 2/5

Which line of code correctly declares an integer pointer p_age and assigns it the address of the integer variable age?

Pointers open up a new level of control in C, allowing for efficient memory management and complex data structures. Mastering them is a key step to becoming a proficient C programmer.