Pointer arithmetic means adding or subtracting integers to or from pointers to move through memory locations of the data type the pointer points to.
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 in memory. For example, if it points to an int, it moves forward by sizeof(int) bytes.
Click to reveal answer
intermediate
What happens when you subtract two pointers?
Subtracting two pointers gives the number of elements between them, not the byte difference. Both pointers must 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
intermediate
Explain how pointer arithmetic relates to array indexing.
Array indexing like arr[i] is actually pointer arithmetic under the hood. It means *(arr + i), moving the pointer i elements forward and accessing that value.
Click to reveal answer
If p is an int* pointing to address 1000 and sizeof(int) is 4, what is the address of p + 2?
A1002
B1008
C1004
D1006
✗ Incorrect
Adding 2 to p moves it 2 elements forward. Each int is 4 bytes, so 2 * 4 = 8 bytes. 1000 + 8 = 1008.
What does the expression (p2 - p1) return if p1 and p2 are pointers to elements in the same array?
AThe byte difference between p2 and p1
BThe sum of the addresses
CAn error
DThe number of elements between p1 and p2
✗ Incorrect
Subtracting pointers returns how many elements apart they are, not the byte difference.
Which of these operations is NOT valid in pointer arithmetic?
Ap1 + p2
Bp - 1
Cp1 - p2
Dp + 3
✗ Incorrect
You cannot add two pointers together. Only add or subtract integers to/from pointers.
What does arr[i] mean in terms of pointers?
Aarr - i
Barr + i
C*(arr + i)
D*(i + arr - 1)
✗ Incorrect
Array indexing is pointer arithmetic: arr[i] is the same as *(arr + i).
If p points to a char and p = p + 5, how many bytes does p move forward?
A5 bytes
B1 byte
CDepends on the system
D10 bytes
✗ Incorrect
A char is 1 byte, so moving 5 elements forward moves 5 bytes.
Describe what happens when you add an integer to a pointer in C.
Think about how arrays and pointers relate.
You got /3 concepts.
Explain why subtracting two pointers gives the number of elements between them, not the byte difference.
Consider how pointer arithmetic is designed for array navigation.
You got /3 concepts.
Practice
(1/5)
1. What does adding 1 to a pointer in C do? int *p;
easy
A. Causes a syntax error
B. Moves the pointer by 1 byte
C. Moves the pointer to the previous element
D. Moves the pointer to the next element of the type it points to
Solution
Step 1: Understand pointer arithmetic basics
In C, 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 the given pointer
Since p is an int*, p + 1 points to the next int in memory.
Final Answer:
Moves the pointer to the next element of the type it points to -> Option D
Quick Check:
Pointer + 1 moves by element size [OK]
Hint: Pointer + 1 moves to next element, not next byte [OK]
Common Mistakes:
Thinking pointer arithmetic moves by 1 byte
Confusing pointer increment with value increment
Assuming pointer addition causes errors
2. Which of the following pointer arithmetic expressions is syntactically correct in C?
int arr[5];
int *p = arr;
easy
A. p + 2
B. p ++ 2
C. p + ++ 2
D. p +* 2
Solution
Step 1: Review pointer arithmetic syntax
Valid pointer arithmetic uses p + n or p - n where n is an integer.
Step 2: Check each option
p + 2 is valid syntax. Options A, C, and D contain invalid operators or spacing causing syntax errors.
Final Answer:
p + 2 -> Option A
Quick Check:
Correct pointer addition syntax is p + n [OK]
Hint: Pointer arithmetic uses '+' or '-' with integers only [OK]
Common Mistakes:
Using invalid operators like '++' incorrectly
Adding spaces that change meaning
Confusing pointer increment with addition
3. What is the output of this code?
int arr[] = {10, 20, 30, 40};
int *p = arr;
printf("%d", *(p + 2));
medium
A. 10
B. 20
C. 30
D. 40
Solution
Step 1: Understand pointer and array relation
Pointer p points to arr[0], which is 10.
Step 2: Calculate pointer offset
*(p + 2) accesses the element 2 positions after arr[0], which is arr[2] = 30.
Final Answer:
30 -> Option C
Quick Check:
Pointer + 2 points to third element = 30 [OK]
Hint: *(p + n) accesses nth element after pointer [OK]
Common Mistakes:
Confusing index starting at 1 instead of 0
Dereferencing pointer without offset
Mixing pointer arithmetic with array indexing
4. Identify the error in this code snippet:
int arr[3] = {1, 2, 3};
int *p = arr + 3;
printf("%d", *p);
medium
A. Syntax error in pointer declaration
B. Pointer points beyond array bounds causing undefined behavior
C. Missing semicolon after array initialization
D. Incorrect printf format specifier
Solution
Step 1: Analyze pointer assignment
p = arr + 3 points to one element past the last valid element (index 3 in a 3-element array).
Step 2: Understand dereferencing invalid pointer
Dereferencing *p here causes undefined behavior because it accesses memory outside the array.