0
0
Cprogramming~5 mins

Pointer arithmetic - 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 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
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
Which of these operations is NOT valid in pointer arithmetic?
Ap1 + p2
Bp - 1
Cp1 - p2
Dp + 3
What does arr[i] mean in terms of pointers?
Aarr - i
Barr + i
C*(arr + i)
D*(i + arr - 1)
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
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.