4.4. Dereferencing A Pointer: The Indirection Operator
Maybe your like
Directly accessing a "normal," non-pointer variable requires only one memory lookup or trip to memory. For example, using the variable i in the simple expression i + 5 requires accessing the memory location named by i once to load the value stored there. However, the dereference or indirection operator requires two memory accesses or lookups. The following illustration builds on the pointer operator examples from the previous section.
| (a) | int i; int* p; // p is a pointer i = 123; p = &i; // p stores the address of i | |
| (b) | cout << *p << endl; | |
- Statements from Figure 2 (a) and (b) in the previous section; p is a pointer variable that points to variable i
- In this context, the asterisk represents the dereference or indirection operator. The expression *p represents the value stored in variable i; so, the complete statement prints the value stored in i, not in p. We can view how the computer dereferences p as four consecutive steps. The computer:
- goes to the memory address represented by the variable name p,
- loads the value stored in p, which is the address of i (0x0a000010 in this example),
- goes to that memory location, and
- loads the value stored at that memory address (123 in this example).
The statement p = &i; makes a connection between the variables i and p. This connection makes it possible to access the value stored in i indirectly through p. That is, it is possible to use or change the value stored in i without using the variable name i! Figure 1 (b) demonstrates how to use the value stored in i indirectly through p, but the indirection operation may also appear on the left side of the assignment operator, making it possible to change the value of i without using variable name i: *p = 321;. Accessing i indirectly through p is called dereferencing p or more generally dereferencing a pointer.
Once the variables i and p are connected, the statement cout << i << " " << *p << endl; prints 123 123 (based on Figure 1 values). The first "123" comes directly from variable i, which only requires one trip to memory. The second "123" comes indirectly from variable i indirectly through p, which requires two trips to memory: one trip to get the address of i and the second trip to get the value stored in i. The following analogy describes the process in more familiar terms and might be easier to follow.
| T data; T* p = &data; ----------------- T* p = new T; | ![]() |
Similarly, a program may create a pointer variable that can point to different data items at different times as the program executes. Alternatively, a program can define a single variable and access it from multiple, convenient locations within the program. Using a pointer, programmers can access the contents of a variable without using its name - they access the contents indirectly through the pointer.
Let's take a moment to review what we have just learned: We now understand the relation between a variable's name, its contents, and its address. We also know the three operators that define a pointer, find the address of a variable, and dereference a pointer. The following simple program demonstrates these operations.
Back | Chapter TOC | NextTag » How To Dereference A Pointer C++
-
C++ Dereferencing - W3Schools
-
C++ Dereferencing Explained - Udacity
-
What Does “dereferencing” A Pointer Mean In C/C++? - Tutorialspoint
-
Pointers
-
How To Dereference A Pointer C++? - Linux Hint
-
C++ Pointers And References
-
How To Dereference A Pointer In C++ - CodeSpeedy
-
In C++ Can We Dereference This Pointer? If So Then How And If Not ...
-
Pointer To Member Operators .* ->* (C++ Only) - IBM
-
C Dereference Pointer - Javatpoint
-
Dereferencing Pointers In C And C++
-
Invalid Pointer Dereference In C++ - Gists · GitHub
-
C++ Pointers – Reference And Dereference Operators
-
Dereferencing Pointers - C++ Video Tutorial | LinkedIn Learning ...
