Homework #12 — Pointers II: Arithmetic
This week we’re going to build a small Pointer Explorer program.
The goal is to practice what we learned in Chapter 11 — Pointers II: Arithmetic of Beej’s Guide to C Programming, up to Array/Pointer Equivalence.
We’ll explore how pointers move through arrays, how pointer arithmetic works, and how we can use pointers instead of array indexes.
1. Create an array
Start with:
int numbers[] = {10, 20, 30, 40, 50};
Create a pointer to its first element:
int *p = numbers;
Print the first element using:
*p
Then print the second element using:
*(p + 1)
Question: Why does adding 1 to p move to the next integer rather than the next byte?
2. Print the entire array using pointers
Write a loop that prints all five elements.
Do not use array indexing inside this loop.
Instead, use pointer arithmetic:
*(p + i)
Expected output:
10
20
30
40
50
Now rewrite the loop so that it increments the pointer itself:
p++;
Compare the two approaches.
What changes when you increment p?
What happens to the original array?
3. Use a sentinel value
Create another array:
int values[] = {11, 22, 33, 44, 55, 999};
The number 999 is our sentinel value.
Write a loop that prints the numbers until it encounters 999.
You must use:
while (*p != 999)
and move the pointer forward after each iteration.
Expected output:
11
22
33
44
55
The sentinel itself should not be printed.
Question: What happens if the sentinel is missing?
Think about this before experimenting. Make sure you understand why the loop must not read beyond the array.
4. Calculate a string length using pointers
Create a function:
int my_strlen(char *s);
It should calculate the length of a C string without calling strlen().
Use two pointers:
- One pointing to the beginning of the string.
- Another moving forward until it encounters
'\0'.
Then subtract the pointers to calculate the number of characters.
Example:
printf("%d\n", my_strlen("Hello"));
Expected output:
5
Try the function with several strings, including an empty string.
Question: Why does subtracting the two pointers give us the length?
For this exercise, assume strings short enough that the result fits in int.
5. Array/pointer equivalence
Start with:
int a[] = {10, 20, 30, 40, 50};
int *p = a;
Print the third element in four different ways:
a[2]
p[2]
*(a + 2)
*(p + 2)
Do all four expressions produce the same result?
Now try this:
2[a]
Does it compile?
What does it print?
Try to explain why using the relationship between array indexing and pointer arithmetic.
6. Write a function that sums an array
Create:
int sum_array(int *p, int count);
The function should return the sum of all elements in an integer array.
Use pointer arithmetic rather than array indexing inside the function.
Example:
int numbers[] = {10, 20, 30, 40, 50};
int result = sum_array(numbers, 5);
printf("%d\n", result);
Expected output:
150
Then change the function parameter from:
int *p
to:
int p[]
Does the function still work?
What does this tell us about arrays passed to functions?
Bonus challenge — Reverse an array
Create:
void reverse_array(int *p, int count);
Reverse the array in place, without creating another array.
For example:
Before: 10 20 30 40 50
After: 50 40 30 20 10
Use two pointers:
- One starting at the beginning.
- One starting at the last element.
Swap the values they point to, then move the pointers toward each other.
Stop when they meet or cross.
You may use a temporary integer variable to perform the swap.
Bonus challenge — Pointer addresses
Print the addresses of all elements in an integer array using %p.
Remember to convert the pointer to void * for %p, as covered in our earlier pointers lesson.
Observe the difference between consecutive addresses.
Then compare that difference with:
sizeof(int)
Try the same experiment with a char array.
Question: Why do pointers to different types advance by different numbers of bytes?
Rules
Use only concepts covered in Learn C With Me so far, up to the material we reached in Chapter 11.
You may use:
- Variables and basic types
- Loops and conditions
- Functions
- Arrays and strings
- Pointers
- Pointer arithmetic
- Structs and
typedef from earlier lessons
You do not need:
- Dynamic memory allocation
malloc() or free()
- Generic
void * pointers
- Concepts from later chapters
What we’re practicing
The most important relationship in this homework is:
a[i] == *(a + i)
We want to understand why those expressions are equivalent, not simply memorize them.
Also remember that pointer arithmetic is only valid within the appropriate array bounds, including the one-past-the-end position, which must not be dereferenced.
As always, experiment, make predictions, compile the code, and compare your expectations with the results.
If something doesn’t work, post your code and compiler diagnostics in the forum thread.