# Pointers – Understanding Memory Addresses

If arrays taught you that data is stored next to each other in memory,  
pointers will teach you **how to access memory directly**.

This is where C becomes powerful and a little scary.

But don’t worry. We’ll go step by step.

By the end of this lesson, you will understand:

*   What a pointer is
    
*   What `&` means
    
*   What `*` means
    
*   How dereferencing works
    
*   Why changing `*p` changes the original variable
    

## 1\. What Is Memory?

When you write:

```c
int n = 98;
```

The computer:

*   Finds a location in memory
    
*   Stores the value 98 there
    
*   Gives that location an address
    

Every variable has:

*   A value
    
*   An address
    

You normally work with the value.

Pointers let you work with the address.

## 2\. Getting the Address of a Variable

To get the address of a variable, use the `&` operator.

Example:

```c
#include <stdio.h>

int main() {
    int n = 98;
    printf("Value of n: %d\n", n);
    printf("Address of n: %p\n", &n);
    return 0;
}
```

`&n` means:

> “Give me the address of n.”

`%p` is used to print memory addresses.

You will see something like:

```plaintext
0x7ffd3c4b8a1c
```

That is the memory location.

## 3\. What Is a Pointer?

A pointer is:

> A variable that stores the address of another variable.

It does not store the value directly.

It stores where the value lives.

* * *

# 4\. Declaring a Pointer

To declare a pointer:

```c
data_type *pointer_name;
```

Example:

```c
int *p;
```

This means:

*   `p` is a pointer
    
*   It will store the address of an integer
    

Important:

The type matters.

If you write:

```c
int *p;
```

Then `p` must point to an `int`.

## 5\. Storing an Address in a Pointer

Example:

```c
int n = 98;
int *p = &n;
```

What happens here?

*   `n` stores 98.
    
*   `&n` gives the address of `n`.
    
*   `p` stores that address.
    

So now:

```plaintext
p → address of n
```

## 6\. Dereferencing a Pointer

The `*` operator has two meanings:

In declaration:

```c
int *p;
```

It means “p is a pointer”.

In usage:

```c
*p
```

It means:

> “Go to the address stored in p and get the value.”

Example:

```c
#include <stdio.h>

int main() {
    int n = 98;
    int *p = &n;

    printf("Value of n: %d\n", n);
    printf("Value using pointer: %d\n", *p);

    return 0;
}
```

`*p` gives you 98.

Because:

*   `p` stores the address of `n`
    
*   `*p` goes to that address
    
*   It retrieves the value stored there
    

## 7\. Modifying a Value Using a Pointer

Here’s where it gets powerful.

```c
#include <stdio.h>

int main() {
    int n = 98;
    int *p = &n;

    *p = 402;

    printf("n = %d\n", n);
    return 0;
}
```

What happens?

*   `p` points to `n`
    
*   `*p = 402` changes the value at that address
    
*   Since that address belongs to `n`, `n` becomes 402
    

So pointers allow you to modify the original variable.

## 8\. Visual Memory Example

Imagine memory like this:

```plaintext
Address: 1000
Value:   98
```

Now:

```plaintext
n → 1000
p → 1000
```

When you write:

```c
*p = 402;
```

You are changing:

```plaintext
Value at address 1000
```

So `n` changes.

## 9\. Why Pointers Matter

Pointers are used for:

*   Working with arrays
    
*   Strings
    
*   Dynamic memory
    
*   Passing variables to functions by reference
    
*   Building data structures
    

Without pointers, C would not be powerful.

## 10\. Common Beginner Mistakes

### ❌ Using a pointer without initializing it

```c
int *p;
*p = 10;   // Dangerous!
```

This causes undefined behavior.

Always assign an address before dereferencing.

### ❌ Confusing `&` and `*`

*   `&` → Get address
    
*   `*` → Go to address
    

### ❌ Forgetting pointer type must match variable type

```c
float f = 3.14;
int *p = &f;  // Wrong
```

Types must match.

## 11\. Practice Exercises

1.  Create an integer variable and print its address.
    
2.  Create a pointer that stores the address of that variable.
    
3.  Use the pointer to change the variable’s value.
    
4.  Draw the memory state on paper.
    

Try this:

```c
int n;
int *p;
n = 98;
p = &n;
*p = 402;
```

Question:

*   What is the final value of `n`?
    
*   Why?
    

## Final Thoughts

Today you learned:

*   Every variable has an address.
    
*   `&` gives the address.
    
*   A pointer stores that address.
    
*   `*p` accesses the value at that address.
    
*   Changing `*p` changes the original variable.
    

Pointers are not magic.

They are simply variables that store memory addresses.

In the next lesson, we will connect pointers and arrays.

And that’s when things really start making sense.
