0
0
C++programming~5 mins

Pointer arithmetic in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AMoves the pointer forward by 2 bytes
BMoves the pointer forward by 2 times the size of double
CMoves the pointer backward by 2 bytes
DCauses a compilation error
What is the result of subtracting two pointers pointing inside the same array?
AThe difference in bytes between the two addresses
BUndefined behavior
CThe number of elements between the two pointers
DThe sum of the two pointers
Which of the following is NOT allowed in pointer arithmetic?
AAdding two pointers
BSubtracting an integer from a pointer
CAdding an integer to a pointer
DSubtracting two pointers
If p is an int* pointing to address 2000 and sizeof(int) is 4, what address does p - 1 point to?
A2004
B1999
C2001
D1996
Why is pointer arithmetic useful?
ATo navigate through elements in arrays efficiently
BTo perform math on memory addresses directly
CTo convert pointers to integers
DTo multiply pointers
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.