# Pointers

### Address of Variables

So far, we've been working with variables like:

{% code title="main.cpp" %}

```cpp
int x = 10;
```

{% endcode %}

But sometimes, we don't want the value itself, rather we will want to know **where is this value stored in memory?**

The variable `x` could have some address like `0x100..`.

<figure><img src="https://2807223923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUqAJOgHs57S8lmqdxRV%2Fuploads%2FN4XxLNp0NPavxIJcIYYV%2FScreenshot%202026-04-04%20at%2011.44.10%E2%80%AFPM.png?alt=media&#x26;token=3dc7cf52-78ca-48a9-aff7-045041551df4" alt=""><figcaption></figcaption></figure>

We don't know so lets find out using the ampersand `&` operator.

{% code title="main.cpp" %}

```cpp
int main() {
    int x = 10;
    std::cout << "location of x: " << &x << std::endl;
}
```

{% endcode %}

<figure><img src="https://2807223923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUqAJOgHs57S8lmqdxRV%2Fuploads%2Fjh1sLUMQLTnyBZLDZfoa%2FScreenshot%202026-04-04%20at%2011.46.24%E2%80%AFPM.png?alt=media&#x26;token=8e06bb7a-f9fb-40f2-95b2-5f841aa2905e" alt=""><figcaption></figcaption></figure>

### Type of Pointers

We know the type of `x` is an `integer` , but what's the type of `&x` ?

Instead of second guessing ourselves, let's rely on the C++ compiler to tell us. C++ offers a [`typeid` function ](https://en.cppreference.com/w/cpp/language/typeid.html)that seems useful:

{% code title="main.cpp" %}

```cpp
int main() {
    int x = 10;
    std::cout << typeid(x).name() << std::endl;
    std::cout << typeid(&x).name() << std::endl;
}
```

{% endcode %}

<figure><img src="https://2807223923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUqAJOgHs57S8lmqdxRV%2Fuploads%2F2mlL018pI1j2qNWvNv4o%2FScreenshot%202026-04-04%20at%2011.52.29%E2%80%AFPM.png?alt=media&#x26;token=554a5a33-dca9-472a-9544-4f89df2c8390" alt=""><figcaption></figcaption></figure>

The output may look cryptic but:

* `i` (as you might have guessed) represents **int**
* `Pi` (which you also might have guessed) represents **pointer to int** (`int*`)

Indeed, we can write it like so:

<table><thead><tr><th>Code</th><th>Visual Aid</th></tr></thead><tbody><tr><td><pre class="language-cpp"><code class="lang-cpp">int main() {
    int x = 10;
    int* p = &#x26;x;
}
</code></pre></td><td><img src="https://2807223923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUqAJOgHs57S8lmqdxRV%2Fuploads%2FwmI1UhUAON7DXsYM0G0W%2FScreenshot%202026-04-04%20at%2011.59.56%E2%80%AFPM.png?alt=media&#x26;token=32bfcc07-917f-48eb-a488-fe21b4fed0bf" alt=""></td></tr></tbody></table>

### De-referencing

Given a pointer of type (`int *`), is there a way I can get the *actual* *value (*&#x61;nd not the address) it points to?

In other words,

{% code title="main.cpp" %}

```cpp
int main() {
    int x = 10;
    int* p = &x;
    
    // can i get 10 using variable p alone?
}
```

{% endcode %}

Yes we can! Since the pointer `p` stores an address, we can dereference the pointer using `*` to get the value! Think of dereferencing as follow the address to get the value stored there.

<table><thead><tr><th>Code</th><th>Visual Aid</th></tr></thead><tbody><tr><td><pre class="language-cpp"><code class="lang-cpp">#include &#x3C;iostream>

int main() {
int x = 10;
int\* p = \&x;
int y = \*p;

```
std::cout &#x3C;&#x3C; "value of x: " &#x3C;&#x3C; x &#x3C;&#x3C; std::endl;
std::cout &#x3C;&#x3C; "value of y: " &#x3C;&#x3C; y &#x3C;&#x3C; std::endl;

return 0;
```

} </code></pre></td><td><img src="https://2807223923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUqAJOgHs57S8lmqdxRV%2Fuploads%2FLFZtftRQtr2L5MWFK3EK%2FScreenshot%202026-04-05%20at%2012.15.36%E2%80%AFAM.png?alt=media&#x26;token=d9b2f0c8-046a-49e1-a8f5-eb03b2501766" alt=""></td></tr></tbody></table>

Realise while both values of x and y are the same, they are **not** pointing to the same 10. They each contain their own version of 10 (and possess their own memory addresses).

<details>

<summary>How do you make <code>x</code> and <code>y</code> to point to the same 10 then?</summary>

Simple: we don't make two variables. We declare 1 variable and multiple pointers to the same variable.

<table><thead><tr><th></th><th></th></tr></thead><tbody><tr><td><pre class="language-cpp" data-title="main.cpp"><code class="lang-cpp">#include &#x3C;iostream>

int main() {
int x = 10;

```
int* y = &#x26;x;   // y points to x
int* z = &#x26;x;   // z also points to x

std::cout &#x3C;&#x3C; "x: " &#x3C;&#x3C; x &#x3C;&#x3C; std::endl;        // 10
std::cout &#x3C;&#x3C; "*y: " &#x3C;&#x3C; *y &#x3C;&#x3C; std::endl;      // 10
std::cout &#x3C;&#x3C; "*z: " &#x3C;&#x3C; *z &#x3C;&#x3C; std::endl;      // 10

*y = 20;  // modify x via pointer y

std::cout &#x3C;&#x3C; "x: " &#x3C;&#x3C; x &#x3C;&#x3C; std::endl;   // 20
std::cout &#x3C;&#x3C; "*z: " &#x3C;&#x3C; *z &#x3C;&#x3C; std::endl; // 20

return 0;
```

} </code></pre></td><td><img src="https://2807223923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUqAJOgHs57S8lmqdxRV%2Fuploads%2F5FVnD4Ke4897qffCUZHA%2FScreenshot%202026-04-05%20at%2012.25.38%E2%80%AFAM.png?alt=media&#x26;token=a3d5ddb2-7ed7-401e-82dc-46dc1ac37bc2" alt=""></td></tr></tbody></table>

</details>

### Pointer-Ception

And yes because pointers also have addresses, we can have *pointers that point to a pointer that points to a value.*

<figure><img src="https://2807223923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUqAJOgHs57S8lmqdxRV%2Fuploads%2FTU4HQFULZxvgyRu87lHu%2Fimage%20(1).png?alt=media&#x26;token=564805f6-fbec-43fc-91f3-7ecd82b52b62" alt=""><figcaption></figcaption></figure>

<table><thead><tr><th>Code</th><th>Output</th></tr></thead><tbody><tr><td><pre class="language-cpp" data-title="main.cpp"><code class="lang-cpp">#include &#x3C;iostream>

int main() {
int x = 10;            // normal variable
int\* p = \&x;           // pointer to x
int\*\* pp = \&p;         // pointer to pointer

```
std::cout &#x3C;&#x3C; "x value: " &#x3C;&#x3C; x &#x3C;&#x3C; std::endl;
std::cout &#x3C;&#x3C; "Address of x (&#x26;x): " &#x3C;&#x3C; &#x26;x &#x3C;&#x3C; std::endl;

std::cout &#x3C;&#x3C; "p (points to x): " &#x3C;&#x3C; p &#x3C;&#x3C; std::endl;
std::cout &#x3C;&#x3C; "Value at p (*p): " &#x3C;&#x3C; *p &#x3C;&#x3C; std::endl;

std::cout &#x3C;&#x3C; "pp (points to p): " &#x3C;&#x3C; pp &#x3C;&#x3C; std::endl;
std::cout &#x3C;&#x3C; "Value at pp (*pp): " &#x3C;&#x3C; *pp &#x3C;&#x3C; std::endl;
std::cout &#x3C;&#x3C; "Value at *pp (**pp): " &#x3C;&#x3C; **pp &#x3C;&#x3C; std::endl;

return 0;
```

} </code></pre></td><td><img src="https://2807223923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUqAJOgHs57S8lmqdxRV%2Fuploads%2FfbUSqjmK3zmusICtqW4M%2FScreenshot%202026-04-05%20at%2012.27.53%E2%80%AFAM.png?alt=media&#x26;token=3a503a72-a98c-4407-8f2f-1026a1c0e747" alt="" data-size="original"></td></tr></tbody></table>
