Pointers
Concepts
Section titled “Concepts”- Memory address
- Pointer
- Pointer type notation
- Getting the address of a variable
- Writing to and reading from an address using dereference (dereferencing)
- Pointer size vs. the size of what it points to
- Why pointers with different element sizes are incompatible
Exercises for understanding
Section titled “Exercises for understanding”Explain what will happen in the following code snippets. Also run the snippets to verify that your reasoning is correct.
When
mainis not shown, place the code in a typicalmainfile:#include <iostream>#include <cstdint>int main(){// your code here}
1. Variable address
Section titled “1. Variable address”int a{ 5 };int* b{ &a }; // int* b = &a;std::cout << b;std::cout << std::endl;Execution trace
Answer:
int* b{ &a } stores the address of the variable a in b.
Next, the address stored in b is printed.
It is not the value at the address stored in b (that would be written as *b), but the address itself.
What is the type of variable b?
Variable-definition syntax consists of:
- a type;
- a variable name;
- optionally, an initialization.
int* b{ &a }; — here:
int*is the type;bis the variable name;{ &a }is the initialization.
The type of b is not int, but int*!
What is the type of expression &a?
& is an operator that obtains the address of a variable.
& applies not to the value of a, but to the variable a itself.
The type of &a is int*.
At compile time, this type indicates
that the resulting address is specifically the address of a variable of type int,
and not of some other type.
&can give the address of any object, but that will be covered in lab 13.
2. Address of an uninitialized variable
Section titled “2. Address of an uninitialized variable”Is something like this allowed?
int a;int* b{ &a };std::cout << b;std::cout << std::endl;Execution trace
Answer:
You can take the address of an uninitialized variable. It will be printed as an ordinary address.
Reading the value at that address would not be allowed.
3. Dereference operator (writing)
Section titled “3. Dereference operator (writing)”int a { 1 };int* b{ &a };*b = 2;std::cout << a;std::cout << std::endl;Execution trace
Answer:
The address of the variable a was stored in b.
In the line *b = 2, *b lets us refer to the variable
located at the address in b, that is, to a.
*b = 2 -> a = 2 writes 2 into a.
4. A number as an address
Section titled “4. A number as an address”Is something like this allowed?
int* b{ 32 };std::cout << *b;std::cout << std::endl;Answer:
No. An address cannot be set directly like this; it must be obtained using
the & operator on an object (for example, a variable).
5. Dereference operator (reading)
Section titled “5. Dereference operator (reading)”int a{ 5 };int b{ *(&a) };std::cout << b;std::cout << std::endl;Correct answer:
Evaluating the expression *(&a):
&abecomes the address of variablea(say, 32).*32follows the address, allowing access to variablea.- When
ais used as an expression, it yields the value 5.
*(&a) -> *32 -> a -> 5
6. Whitespace in pointer declarations
Section titled “6. Whitespace in pointer declarations”int value{ 5 };int* a{ &value };int *b{ &value };int * c{ &value };int * d{ &value }; * a = 6;std::cout << *b << std::endl;std::cout << *c << std::endl;std::cout << * d << std::endl;Answer
It prints 6 three times.
int* a, int *b, int * c, and int * d all declare an int*.
Whitespace between these tokens does not change the declaration.
All four pointers store the address of the same variable.
Whitespace around the dereference operator also has no effect: *a and * a access the same variable.
7. Printing a complex expression
Section titled “7. Printing a complex expression”int a{ 5 };int* b{ &a };std::cout << (*b) + 7;std::cout << std::endl;Execution trace
Correct answer:
(*b) + 7 is an expression. It is evaluated in parts:
(*b)means following the address inband treating the result as the variablea.(*b) + 7->a + 7;ais replaced with the value inabecause it is used as an expression.5 + 7->12.
8. Pointer to a larger data type
Section titled “8. Pointer to a larger data type”uint8_t a{ 5 };int* b{ &a };Execution trace
Correct answer:
Compilation error (see the video about pointers)
9. Pointer to a smaller data type
Section titled “9. Pointer to a smaller data type”int a = 5;uint8_t* b = &a;Execution trace
Correct answer:
Compilation error (see the video about pointers)
10. Dependence of the address on the value
Section titled “10. Dependence of the address on the value”Will b and c contain the same address?
int a = 5;int* b = &a;a = 6;int* c = &a;Execution trace
Correct answer:
They will contain the same address.
Variables never change their address.
a = 6 writes 6 into the existing memory cell.
It does not redirect a to another cell.
&a takes the address of cell a, not the value in it.
It will always give the same address, regardless of
which value is stored in a.
11. It is the same memory!
Section titled “11. It is the same memory!”int a = 5;int* ap = &a;
*ap = 6;std::cout << a;std::cout << std::endl;
a = 7;std::cout << *ap;std::cout << std::endl;Execution trace
Answer:
Both reads and writes occur at the same address.
ap contains the address of variable a.
Writing to or reading from *ap is equivalent to working with a directly.
12. Assigning a value through a pointer
Section titled “12. Assigning a value through a pointer”Is something like this allowed?
int a;int* b = &a;*b = 5;std::cout << a;std::cout << std::endl;Execution trace
Answer:
A value can be assigned to an uninitialized variable through a pointer. This is allowed.
13. Reassigning a pointer
Section titled “13. Reassigning a pointer”int a = 5;
int* p = &a;*p = 6;
int b = 7;
p = &b;*p = 8;
std::cout << a;std::cout << std::endl;
std::cout << b;std::cout << std::endl;Execution trace
Answer:
On the line p = &b, the address in p itself is overwritten with the address of another variable (b).
*p = 8 now writes 8 into b.
14. Double pointer
Section titled “14. Double pointer”int a = 5;int b = 6;int* p = &a;int** pp = &p;**pp = 7;
*pp = &b;**pp = 8;
std::cout << a;std::cout << std::endl;
std::cout << b;std::cout << std::endl;Execution trace
Answer
int a = 5; // say, address = 32int b = 6; // say, address = 36int* p = &a; // address of p = 40, address stored in p = 32int** pp = &p; // address stored in pp = 40**pp = 7; // *(*pp) --> *(40) --> *(p) --> *32 --> a // so a = 7*pp = &b; // address stored in p = 36**pp = 8; // *(*pp) --> *(40) --> *(p) --> *36 --> b // so b = 8
std::cout << a;std::cout << std::endl;
std::cout << b;std::cout << std::endl;15. auto from an address
Section titled “15. auto from an address”int value{ 5 };auto p{ &value };*p = 8;std::cout << value << std::endl;Execution trace
Answer
&value has type int*, so p has type int*.
It prints 8: writing through the inferred pointer changes value.
16. auto from a pointer and its value
Section titled “16. auto from a pointer and its value”int value{ 5 };int* p{ &value };auto copiedPointer{ p };auto copiedValue{ *p };*copiedPointer = 8;std::cout << value << std::endl;std::cout << copiedValue << std::endl;Execution trace
Answer
p has type int*; *p has type int.
Thus copiedPointer points to the same variable, while copiedValue is an independent integer copy.
It prints 8 and 5.
17. auto from the address of a pointer
Section titled “17. auto from the address of a pointer”int value{ 5 };int* p{ &value };auto pp{ &p };**pp = 9;std::cout << value << std::endl;Execution trace
Answer
&p has type int**, so pp has type int**.
*pp accesses p; **pp accesses value.
It prints 9.
18. Pointer sizes
Section titled “18. Pointer sizes”int a = 7;int* pa = &a;void* voidp = pa;
uint8_t c = 9;uint8_t* pc = &c;
std::cout << sizeof(pa);std::cout << std::endl;
std::cout << sizeof(voidp);std::cout << std::endl;
std::cout << sizeof(pc);std::cout << std::endl;Answer
Pointers of any type have the same size because they only store memory addresses.
On 64-bit systems, any pointer is 64 bits in size (you are most likely on a 64-bit system).
19. Pointer and variable sizes
Section titled “19. Pointer and variable sizes”int a = 7;int* ap = &a;
std::cout << sizeof(a);std::cout << std::endl;
std::cout << sizeof(ap);std::cout << std::endl;
std::cout << sizeof(*ap);std::cout << std::endl;Answer
sizeof(a) is the same as sizeof(int), 4.
sizeof(ap) is the same as sizeof(int*), 8.
sizeof(*ap) does not evaluate the expression *ap; it only determines the type of its result.
The expression *ap has type int, so sizeof(int), which is 4, is calculated.
20. Pointer to itself
Section titled “20. Pointer to itself”void* p = nullptr;p = static_cast<void*>(&p);
std::cout << p;std::cout << std::endl;
std::cout << &p;std::cout << std::endl;Answer
&p takes the address of the variable p itself.
The type of the expression &p is void** (the address of a variable of type void*).
The static_cast<void*>(...) explicitly converts this void** to void*,
that is, to the type of the variable p itself.
So the assignment p = static_cast<void*>(&p) stores
the address of p itself in p — now p points to itself.
Two identical addresses are printed: the value stored in p matches the address &p.
If the static_cast<void*> is omitted, the conversion from void** to void* still happens implicitly.
21. Empty pointer initialization
Section titled “21. Empty pointer initialization”int* p{};std::cout << p << std::endl;Execution trace
Answer
Empty braces value-initialize the pointer, which for pointers means it becomes nullptr.
This is equivalent to writing int* p{ nullptr };.
So 0 is printed (the null address).
This is different from int* p;, which leaves p uninitialized with garbage data.
Unlike an uninitialized pointer, reading p itself here (for example, printing it) is allowed.
Dereferencing p itself would still not be allowed, since it does not point to any variable.
22. Copying a variable and a pointer with similar names
Section titled “22. Copying a variable and a pointer with similar names”int x{ 0 };int* px{ &x };int y{ x };int* py{ px };
*px = 1;
std::cout << x << std::endl;std::cout << y << std::endl;std::cout << *px << std::endl;std::cout << *py << std::endl;Execution trace
Answer:
int y{ x } copies the value from x — y is independent of x afterwards,
so writing through the pointer does not affect it.
int* py{ px } copies the address from px — both pointers point to the same variable x.
*px = 1 writes 1 into x, so the output is:
x—1,y—0(copied before the assignment),*px—1,*py—1(same address aspx).
Type-checking puzzles
Section titled “Type-checking puzzles”Each snippet is independent. Does it compile? Give the types of both sides of the last assignment. Most snippets are deliberately incorrect; explain a correction instead of trying to run them.
1. An integer assigned to a pointer
Section titled “1. An integer assigned to a pointer”int value{ 5 };int* p{};p = value;Execution trace
Answer
It does not compile.
The right side has type int; the left side requires int*.
Use p = &value; to store an address.
2. A pointer assigned to a double pointer
Section titled “2. A pointer assigned to a double pointer”int value{ 5 };int* p{ &value };int** pp{};pp = p;Execution trace
Answer
It does not compile.
p has type int*, while pp requires int**.
Use pp = &p; to point to the pointer variable.
3. A pointer assigned to an integer
Section titled “3. A pointer assigned to an integer”int value{ 5 };int* p{ &value };int result{};result = p;Execution trace
Answer
It does not compile.
p is an address of type int*, not an integer value.
Use result = *p; to copy the pointed-to value.
4. A pointer address assigned to a pointer
Section titled “4. A pointer address assigned to a pointer”int value{ 5 };int* p{ &value };int* other{};other = &p;Execution trace
Answer
It does not compile.
&p has type int**; other has type int*.
Use other = p; to copy the stored address, or declare other as int** to store &p.
5. An integer address assigned to a double pointer
Section titled “5. An integer address assigned to a double pointer”int value{ 5 };int** pp{};pp = &value;Execution trace
Answer
It does not compile.
&value has type int*, not int**.
An extra * in a declaration does not make it accept every kind of address.
6. Copying a pointer through a double pointer
Section titled “6. Copying a pointer through a double pointer”int value{ 5 };int* p{ &value };int** pp{ &p };int* other{};other = *pp;Execution trace
Answer
It compiles.
*pp has type int*, so the assignment is valid.
other and p now point to the same integer.
7. One dereference instead of two
Section titled “7. One dereference instead of two”int value{ 5 };int* p{ &value };int** pp{ &p };int result{};result = *pp;Execution trace
Answer
It does not compile.
*pp has type int*; result needs int.
Use result = **pp; to read the integer.
8. Writing through two dereferences
Section titled “8. Writing through two dereferences”int value{ 5 };int* p{ &value };int** pp{ &p };**pp = 8;Execution trace
Answer
It compiles.
**pp accesses the int variable value.
The assignment is valid and stores 8 in it.
9. A size_t address assigned to an int8_t pointer
Section titled “9. A size_t address assigned to an int8_t pointer”#include <cstddef>#include <cstdint>size_t count{ 3 };int8_t* p{};p = &count;Execution trace
Answer
It does not compile.
&count has type size_t*, not int8_t*.
Pointers preserve the pointed-to type; this assignment performs no numeric conversion.
10. An int8_t address assigned to a size_t pointer
Section titled “10. An int8_t address assigned to a size_t pointer”#include <cstddef>#include <cstdint>int8_t value{ 3 };size_t* p{};p = &value;Execution trace
Answer
It does not compile.
&value has type int8_t*; p requires size_t*.
Having a numeric value that fits both types does not make their pointers compatible.
11. An int8_t value assigned to a size_t
Section titled “11. An int8_t value assigned to a size_t”#include <cstddef>#include <cstdint>int8_t value{ 3 };size_t count{};count = value;Execution trace
Answer
It compiles.
This is a numeric assignment, so the value is converted to size_t.
count becomes 3.
It does not assign or reinterpret an address.
12. A size_t value assigned to a pointer
Section titled “12. A size_t value assigned to a pointer”#include <cstddef>size_t count{ 3 };size_t* p{};p = count;Execution trace
Answer
It does not compile.
count has type size_t; p requires size_t*.
A type being large enough to hold numbers does not make those numbers pointers.
13. A double pointer with a different pointed-to type
Section titled “13. A double pointer with a different pointed-to type”#include <cstddef>#include <cstdint>size_t count{ 3 };size_t* p{ &count };int8_t** pp{};pp = &p;Execution trace
Answer
It does not compile.
&p has type size_t**, while pp requires int8_t**.
Matching the number of stars is not enough: the pointed-to types must also match.
14. A size_t address with the wrong pointer depth
Section titled “14. A size_t address with the wrong pointer depth”#include <cstddef>size_t count{ 3 };size_t** pp{};pp = &count;Execution trace
Answer
It does not compile.
&count has type size_t*, not size_t**.
First create a pointer to count, then take the address of that pointer.
15. Assigning an int8_t read through a pointer to a size_t
Section titled “15. Assigning an int8_t read through a pointer to a size_t”#include <cstddef>#include <cstdint>int8_t small{ 3 };int8_t* p{ &small };size_t count{};count = *p;Execution trace
Answer
It compiles.
*p has type int8_t, so this converts a numeric value to size_t.
count becomes 3; the pointer itself is not assigned.