> For the complete documentation index, see [llms.txt](https://learncpp.benntan.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learncpp.benntan.com/memory-and-ownership/pointers.md).

# 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..`.&#x20;

<figure><img src="https://1407522535-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgMKUM5bJORzyfGuRLJG7%2Fuploads%2FnZUGBzKxpys7S4bzQL67%2FScreenshot%202026-04-04%20at%2011.44.10%E2%80%AFPM.png?alt=media&amp;token=e43635fb-2d32-4c3d-ac50-9009c887e1f0" 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://1407522535-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgMKUM5bJORzyfGuRLJG7%2Fuploads%2FPD2qi9T4kRKF35lEEC7c%2FScreenshot%202026-04-04%20at%2011.46.24%E2%80%AFPM.png?alt=media&amp;token=f68eb140-468a-4134-88f4-cd60255a920f" alt=""><figcaption></figcaption></figure>

### Type of Pointers

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

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://1407522535-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgMKUM5bJORzyfGuRLJG7%2Fuploads%2FIEwBlHA5qY6F1meUD4La%2FScreenshot%202026-04-04%20at%2011.52.29%E2%80%AFPM.png?alt=media&amp;token=a7398563-ced5-497a-afcf-79af3eaf7fd3" 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://1407522535-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgMKUM5bJORzyfGuRLJG7%2Fuploads%2FoFLv9G7XuoFfKTdnRKR4%2FScreenshot%202026-04-04%20at%2011.59.56%E2%80%AFPM.png?alt=media&amp;token=bf80117e-105f-4d32-8fe8-75237f4f6aaf" 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?&#x20;

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">int main()
{
    int x = 10;
    int* p = &#x26;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;
```

} </code></pre></td><td><img src="https://1407522535-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgMKUM5bJORzyfGuRLJG7%2Fuploads%2FBY2RLYIfZUkWUa2CJWk3%2FScreenshot%202026-04-05%20at%2012.15.36%E2%80%AFAM.png?alt=media&amp;token=fa6b774c-f972-4fe9-9842-a810aa00afa5" 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).&#x20;

<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 = \&x;   // y points to x
int\* z = \&x;   // z also points to x

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

*y = 20;  // modify via pointer

std::cout &#x3C;&#x3C; x &#x3C;&#x3C; std::endl;   // 20 (x changed!)
```

} </code></pre></td><td><img src="https://1407522535-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgMKUM5bJORzyfGuRLJG7%2Fuploads%2FvJWqe76vAFSE7i6ZrKpq%2FScreenshot%202026-04-05%20at%2012.25.38%E2%80%AFAM.png?alt=media&amp;token=1ebdf77d-6ddb-448f-859c-b8aaac179726" 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://1407522535-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgMKUM5bJORzyfGuRLJG7%2Fuploads%2F7cYKAtgdbhfv6kDn0lUr%2Fimage.png?alt=media&amp;token=21f48cfc-4db5-4f6e-a897-8ca56261aaf5" 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"><strong>#include &#x3C;iostream>
</strong>
int main() {
    int x = 10;            // normal variable
    int* p = &#x26;x;           // pointer to x
    int** pp = &#x26;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://1407522535-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FgMKUM5bJORzyfGuRLJG7%2Fuploads%2FY6ro4t9GBooZVeYnqrKL%2FScreenshot%202026-04-05%20at%2012.27.53%E2%80%AFAM.png?alt=media&amp;token=9cae4917-f800-4514-bc6a-bb00a694b928" alt="" data-size="original"></td></tr></tbody></table>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://learncpp.benntan.com/memory-and-ownership/pointers.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
