# Pointer- The deference operator

The ***\* symbol*** (asterisk) can be used as

* pointer symbol
* dereference symbol
* (and also in multiplication, e.g: 3\*5 = 15)

So what is a dereference? Consider the following code sample.

```
    int num = 3;            // a simple variable holding the value 3
    int *pNum = &num;        // a pointer holding the address of num
    cout << pNum << endl;    // output the memory address of num
    cout << *pNum << endl;    // output the value 3
```

In the first line, we declare a variable called num and assign it the value 3. Next we create a pointer \*pNum and assign it the memory address of the variable num. The first cout statement outputs the address of num, because that is what the pointer variable pNum holds. But the last line outputs the value 3. Why? Here is where the power and the danger of pointers starts to become apparent.

Using the ***dereference operator, you can gain direct access to the underlying value in the variable num***. If you are unsure what this means, lets add another code segment to help clarify. Copy and paste this code into a C++ app to see it working on your system if you want to test it.

```
    int num = 3;
    int *pNum = &num;
    cout << pNum << endl;

    //the deference operator "de-references" the memory address to get at the underlying value stored there
    cout << *pNum << endl; 

    //here will show how using the derefence operator allows us to change the underlying value stored in num
    *pNum = 45;
    cout << *pNum << endl;  //45
    cout << num << endl;    //45, so that pNum has the address of num, then if use the dereference operator *
                            //(by assigning *pNum = 45) then value of num changes!!
```

What is being demonstrated here is that we are ***using the dereference operator to change the underlying value of num***, indirectly. The two last cout lines demonstrate that the value of num and \*pNum are precisely the same value (***=45***). You might be thinking at this point that we could simply have changed the value of num directly and you would be correct. But hold that thought for a moment and we'll revisit the importance of this later in the lesson when we discuss the purpose of pointers and their uses.

Note: & is called "***amphesand***"

***Derefence operator*** (sometimes considered as direct access)

```
int contents = *pNum;
```

How to differentiate:

```
//Asterisk * is on the left of the assignment: pointer
int *pNum = &num; //Assign the address of num bo the variable pNum (pointer variable)

//Asterisk * is on the right side of the assignment: dereference operator
int contents = *pNum;

//Becareful, the following code demonstrates how dereference operator is used for change the underlying
//value stored in pNum, e.g:
*pNum = 45;  //also on the left side of the assignment
```


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lamhung81-2.gitbook.io/learning-c/the-deference-operator.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
