Recall & Review
beginner
What is pointer arithmetic in C++?
Pointer arithmetic means adding or subtracting integers to or from pointers to move through memory locations of the pointed data type.
Click to reveal answer
beginner
How does adding 1 to a pointer affect its value?
Adding 1 to a pointer moves it to the next element of its type, not just the next byte. For example, if it points to an int (4 bytes), adding 1 moves it 4 bytes forward.
Click to reveal answer
intermediate
What happens when you subtract two pointers?
Subtracting two pointers gives the number of elements between them, assuming they point to elements of the same array.
Click to reveal answer
beginner
Why can't you add two pointers together?
Adding two pointers is not allowed because it doesn't make sense to combine two memory addresses. Pointer arithmetic only supports adding or subtracting integers to pointers.
Click to reveal answer
beginner
Example: If int* p points to address 1000 and sizeof(int) is 4, what is the address after p + 3?
The address will be 1000 + 3 * 4 = 1012 because pointer arithmetic moves in steps of the size of the pointed type.
Click to reveal answer
What does adding 2 to a pointer of type double* do?
✗ Incorrect
Adding 2 to a double* pointer moves it forward by 2 times the size of double in memory.
What is the result of subtracting two pointers pointing inside the same array?
✗ Incorrect
Subtracting two pointers gives the number of elements between them, not bytes.
Which of the following is NOT allowed in pointer arithmetic?
✗ Incorrect
Adding two pointers is not allowed in C++ pointer arithmetic.
If p is an int* pointing to address 2000 and sizeof(int) is 4, what address does p - 1 point to?
✗ Incorrect
Subtracting 1 moves the pointer back by 4 bytes, so 2000 - 4 = 1996.
Why is pointer arithmetic useful?
✗ Incorrect
Pointer arithmetic helps move through array elements by adjusting the pointer by element sizes.
Explain how pointer arithmetic works when adding an integer to a pointer.
Think about how arrays are stored in memory.
You got /3 concepts.
Describe what happens when you subtract two pointers pointing to elements in the same array.
Consider how many steps separate the two pointers.
You got /3 concepts.