What is a Pointer?

A pointer is like a GPS coordinate for your computer's memory! Just as a GPS coordinate tells you exactly where to find a location in the real world, a pointer tells your program exactly where to find a piece of data in your computer's memory.

In simple terms, a pointer is a variable that stores the memory address of another variable, rather than storing a value directly.

Why Do We Need Pointers?

Think of memory as a huge apartment building with numbered rooms. Each room (memory location) has an address, and each room can store some data. Instead of moving furniture (data) around, wouldn't it be easier to just pass around the room number? That's exactly what pointers do!

Pointers are useful for:

  • Efficient memory management
  • Dynamic memory allocation
  • Passing large data structures to functions
  • Creating linked lists, trees, and other data structures

Declaring Pointers

To declare a pointer, we use the * (asterisk) symbol:

int *ptr;        // Pointer to an integer
char *charPtr;   // Pointer to a character
float *floatPtr; // Pointer to a float

The Address-of Operator (&)

The & operator gets the memory address of a variable:

#include <stdio.h>

int main() {
    int num = 42;
    int *ptr;
    
    ptr = &num;  // ptr now stores the address of num
    
    printf("Value of num: %d\n", num);
    printf("Address of num: %p\n", &num);
    printf("Value stored in ptr: %p\n", ptr);
    
    return 0;
}

The Dereference Operator (*)

The * operator (when used with an existing pointer) accesses the value at the memory address:

#include <stdio.h>

int main() {
    int num = 42;
    int *ptr = &num;
    
    printf("Value of num: %d\n", num);
    printf("Value accessed through ptr: %d\n", *ptr);
    
    // We can also modify the value through the pointer
    *ptr = 100;
    printf("New value of num: %d\n", num);
    
    return 0;
}

Pointer Operations Summary

OperationSymbolDescriptionExample
Declaration*Declares a pointer variableint *ptr;
Address-of&Gets the address of a variableptr = &num;
Dereference*Accesses value at the address*ptr = 10;

A Simple Example

Let's put it all together with a complete example:

#include <stdio.h>

int main() {
    // Regular variable
    int age = 25;
    
    // Pointer declaration and initialization
    int *agePtr = &age;
    
    printf("=== Before modification ===\n");
    printf("age = %d\n", age);
    printf("address of age = %p\n", &age);
    printf("agePtr = %p\n", agePtr);
    printf("*agePtr = %d\n", *agePtr);
    
    // Modify value through pointer
    *agePtr = 30;
    
    printf("\n=== After modification ===\n");
    printf("age = %d\n", age);
    printf("*agePtr = %d\n", *agePtr);
    
    return 0;
}

Key Points to Remember

  • A pointer stores a memory address, not a value
  • Use & to get the address of a variable
  • Use * to access the value at the address stored in a pointer
  • The pointer and the original variable refer to the same memory location
  • Modifying *ptr changes the original variable's value

Think of it this way: if a variable is a house, then a pointer is the house's address written on a piece of paper. You can use that address to find and visit the house anytime!