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
✗ 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?
AThe difference in bytes between the two addresses
BUndefined behavior
CThe number of elements between the two pointers
DThe sum of the two pointers
✗ Incorrect
Subtracting two pointers gives the number of elements between them, not bytes.
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
✗ 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?
A2004
B1999
C2001
D1996
✗ Incorrect
Subtracting 1 moves the pointer back by 4 bytes, so 2000 - 4 = 1996.
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
✗ 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.
Practice
(1/5)
1. What does adding 1 to a pointer in C++ do when the pointer points to an int array?
easy
A. Moves the pointer to the next integer element in the array
B. Increases the pointer's memory address by 1 byte
C. Moves the pointer to the next byte in memory
D. Causes a compilation error
Solution
Step 1: Understand pointer arithmetic basics
Adding 1 to a pointer moves it to the next element of the type it points to, not just 1 byte.
Step 2: Apply to int pointer
Since int usually occupies 4 bytes, adding 1 moves the pointer by 4 bytes to the next integer.
Final Answer:
Moves the pointer to the next integer element in the array -> Option A
Quick Check:
Pointer + 1 = next element [OK]
Hint: Pointer + 1 moves to next element, not next byte [OK]
Common Mistakes:
Thinking pointer arithmetic adds 1 byte always
Confusing pointer increment with byte increment
Assuming pointer arithmetic causes errors
2. Which of the following pointer arithmetic expressions is syntactically correct in C++?
easy
A. 1 + ptr
B. All of the above
C. ptr - 1
D. ptr + 1
Solution
Step 1: Check pointer addition syntax
Adding an integer to a pointer like ptr + 1 is valid and moves the pointer forward.
Step 2: Check commutativity and subtraction
Integer + pointer like 1 + ptr is also valid due to commutativity. Pointer - integer like ptr - 1 moves pointer backward and is valid.
Final Answer:
All of the above -> Option B
Quick Check:
Pointer + int and int + pointer both valid [OK]
Hint: Pointer + int and int + pointer both work [OK]
Common Mistakes:
Assuming int + pointer is invalid
Thinking pointer - int is not allowed
Confusing pointer arithmetic with pointer subtraction
3. What is the output of the following C++ code?
int arr[] = {10, 20, 30, 40};
int *p = arr;
int *q = arr + 3;
std::cout << q - p << std::endl;
medium
A. 3
B. 4
C. 30
D. Compilation error
Solution
Step 1: Understand pointer subtraction
Subtracting two pointers gives the number of elements between them, not bytes.
Step 2: Calculate difference between pointers
q = arr + 3 points to the 4th element, p = arr points to the 1st. Difference is 3 elements.
Final Answer:
3 -> Option A
Quick Check:
Pointer difference = number of elements [OK]
Hint: Pointer subtraction returns element count, not bytes [OK]