Back to List

Pointers and Memory Addresses

Learn what a pointer is, why memory addresses are important, and how references differ from pointers in high-level languages.

Intermediate
|
12min
|
Verified (2026-07)
pointermemory addressreferenceindirect accessNULL pointer
Progress0/23 (0%)

Pointers and Memory Addresses

After completing this topic, you will:

Understand that a pointer is a variable that stores a memory address, and know why it is important in computer science and how it is hidden in high-level languages.


Where do variables live?

When a program runs, variables are stored in memory. Memory is a sequence of bytes, each with a unique number. This number is the memory address.

text
Memory address:  0x1000  0x1004  0x1008  0x100C
             β”Œβ”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”€β”β”Œβ”€β”€β”€β”€β”€β”€β”
             β”‚  42  β”‚β”‚  7   β”‚β”‚ 'A'  β”‚β”‚  0   β”‚
             β””β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”˜β””β”€β”€β”€β”€β”€β”€β”˜
              Variable a   Variable b   Variable c   Variable d

When you write a = 42, the computer stores the value 42 somewhere in memory, and associates the name a with that location. We don't need to worry about the address; we just use the name a.


Pointers: variables that store addresses

Regular variables store values. Pointers store the address of another variable.

c
int a = 42;       // Store 42 in a
int *p = &a;      // Store the address of a in p

printf("%d\n", a);    // 42 (the value of a)
printf("%p\n", p);    // 0x1000 (the address of a)
printf("%d\n", *p);   // 42 (the value at the address pointed to by p)
text
Variable a          Pointer p
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    42    β”‚   β”‚  0x1000  │──→ Points to a
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
Address: 0x1000    Address: 0x2000
  • &a β€” get the address of a (address-of)
  • *p β€” dereference p (get the value at the address p points to)

Why do we need pointers?

1. Efficiently pass large data

Suppose you have an array of 10 million numbers and want to pass it to a function. Copying the entire array would be wasteful in terms of memory and time. Passing just the address allows you to access the original data directly.

2. Dynamic data structures

Linked lists, trees, graphs β€” these data structures need to store the "location of the next node". That "location" is a pointer.

text
Node A              Node B              Node C
β”Œβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”
β”‚ 10 β”‚ 0x2000 │──→│ 20 β”‚ 0x3000 │──→│ 30 β”‚ NULL β”‚
β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”˜

3. Modify the original in a function

c
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int x = 10, y = 20;
swap(&x, &y);
// x = 20, y = 10

Without pointers, if you pass values by copy, any changes made inside the function will not affect the original variables. Pointers allow you to modify the original data.


Dangerous pointers

Null pointers

c
int *p = NULL;
printf("%d", *p);  // Crash! (Segmentation Fault)

Dereferencing a pointer that points to nothing will crash your program. This is called a null pointer dereference β€” one of the most costly types of bugs in the history of bugs.

Dangling pointers

c
int *p = malloc(sizeof(int));
*p = 42;
free(p);        // Return the memory
printf("%d", *p);  // Memory has already been freed β€” unpredictable

After freeing the memory using free, if you access the memory at that address, you might get garbage values or the program might crash.


Pointers in high-level languages

High-level languages like Python, JavaScript, and Java do not let you work with pointers directly. However, the same concept exists under the name references.

python
# References in Python
a = [1, 2, 3]
b = a # b "references" the same list
b.append(4)
print(a) # [1, 2, 3, 4] β€” the original is also modified

In Python, variables are essentially pointers. However, there is no syntax like & or *, and you cannot directly manipulate memory addresses. The problem discussed in the previous Python topic (reference propagation of mutable objects) is related to this.

C PointersPython References
Direct access to addressYesNo
Pointer arithmeticYes (p + 1)No
NULL crashYesNone β†’ AttributeError
Manual memory deallocationRequired (free)Automatic (garbage collection)

High-level languages hide pointers and make them safer. The trade-off is that you lose the ability to directly control memory.


Key takeaways

Pointers are variables that store memory addresses. They are necessary for passing large data, implementing dynamic data structures, and modifying the original data in a function. Python/JavaScript "references" are a safer way to achieve the same thing as pointers β€” you can't manipulate addresses directly, but the underlying principle is the same.

πŸ’¬ Questions & Comments

0 comments

You can post without signing in. Guest comments cannot be edited or deleted by their author.

0/2000

Loading...