Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.
Pointer Arithmetic Examples in Embedded C: Strings, Checksums, Ring Buffers, Matrices and Filters
Eight worked examples with real output: strlen and strcpy, byte and word checksums, two-pointer reversal, a ring buffer, matrix row pointers, overlapping copies, a number-list parser and a sliding-window average. Study this one of the pointer arithmetic examples in embedded C closely.
Pointer Arithmetic Examples in Embedded C: What This Page Covers
These pointer arithmetic examples in embedded C apply Lesson 9.2: string routines written with pointers, byte and word checksums, in-place reversal with two pointers, a ring buffer with head and tail pointers, matrix rows, overlapping copies, a number-list parser and a sliding-window average. Every program was compiled with GCC and run; the output is real. These pointer arithmetic examples in embedded C build on each other.
| # | Example | Skill |
|---|---|---|
| 1 | strlen and strcpy with pointers | *p++ |
| 2 | Checksums over bytes and words | Step size |
| 3 | Reverse and palindrome with two pointers | Converging pointers |
| 4 | A ring buffer with head and tail pointers | Wrap-around |
| 5 | Matrix row sums with row pointers | 2D arrays |
| 6 | Overlapping copies: forward or backward? | memmove logic |
| 7 | Parsing a number list with a cursor | Pointer advance |
| 8 | Sliding-window average | Pointer difference |
| 9 | Mini exercises | Self-test |
Example 1: strlen and strcpy with Pointers
Problem: Re-implement two standard string functions with pointer arithmetic. my_strlen advances a copy of the pointer until it finds the zero byte; the difference p - s is the length. my_strcpy uses the classic *d++ = *src++ loop: the assignment expression has the value that was copied, so the loop stops after the terminator has been written. The destination must be large enough (here 16 bytes for 10 including the terminator); the function cannot check it, which is why size-limited versions exist (Lesson 8.2). Among the pointer arithmetic examples in embedded C this pattern is very common.
strncpy/snprintf with careful termination. The loops above show how they work, not how to write a production API.Example 2: Checksums over Bytes and Words
Problem: Compute simple checksums for a frame and for a table in Flash. The byte versions step one byte at a time; sum32_words takes a uint32_t *, so *p++ moves four bytes per step and the count is in words. The loop idiom while (n--) runs exactly n times. Sums of unsigned values wrap around by definition: 0x100 + 0x200 + 0xFFFFFFFF + 2 = 0x301 modulo 232. The 8-bit sum of 01+02+03+F0+0F is 0x105, which truncates to 0x05, while the xor of the same bytes is 0xFF: different checksums give different values for the same data. The last line reads the same table as 16 bytes through a byte pointer. Keep these pointer arithmetic examples in embedded C for revision.
Example 3: Reverse and Palindrome with Two Pointers
Problem: Process an array from both ends. Two pointers start at the first and last element and move towards each other; the loop condition lo < hi compares them (same array, so the comparison is valid) and stops when they meet or cross, which works for even and odd lengths without special cases. *lo++ = *hi and *hi-- combine the access and the step. The same pattern appears in reversing byte order of a buffer and in checking symmetric patterns.
n == 0 the expression s + n - 1 points before the array, which is undefined. Guard empty input before calling, or use an index loop.Example 4: A Ring Buffer with Head and Tail Pointers
Problem: Buffer UART bytes between an interrupt and the main loop. The producer writes at head, the consumer reads at tail; each pointer wraps to the start when it reaches buf + CAP. A separate counter distinguishes full from empty (when head equals tail the buffer could be either). The fifth push is rejected because the buffer holds four bytes; after two pops the pushes of 6 and 7 wrap around to the beginning of the array. Draining returns the data in first-in first-out order: 3, 4, 6, 7.
used, the pointers) must be handled with volatile and atomic updates, or by using indices owned by one side each (Module 14).Example 5: Matrix Row Sums with Row Pointers
Problem: Sum the rows and columns of a small matrix, such as a 3 × 4 block of sensor readings. The parameter int (*m)[COLS] is a pointer to a row of four ints, so m[r] is the row and m[r][c] the element; the column count is part of the type, which lets the compiler scale the row step. Inside row_sums a plain int * walks one row. The explicit cast in main adds const to the row type: strictly, ISO C up to C17 does not allow the implicit conversion from int (*)[4] to const int (*)[4], and GCC reports it with -pedantic, so the cast keeps the code portable. Sizes confirm the layout: 12 ints = 48 bytes, one row = 16 bytes.
Example 6: Overlapping Copies, Forward or Backward?
Problem: Shift part of a buffer inside itself (inserting bytes in a message, scrolling a display line). If the destination lies above the source and the areas overlap, a forward copy overwrites source bytes before they are read; the loop must run backward. The program shifts “ABCDE” two places to the right: the correct result is ABABCDEHI, while the naive forward loop produces ABABABAHI because the first two bytes are copied again and again. Shifting left is safe forward. This decision is exactly what the library function memmove makes; memcpy does not and has undefined behavior for overlapping areas.
memmove whenever source and destination can overlap; use memcpy only when you know they cannot.Example 7: Parsing a Number List with a Cursor
Problem: Read comma-separated settings sent over a serial line. The cursor p skips spaces, strtol converts digits and stores in end the address just after the number (a pointer to pointer, Lesson 9.3), and the loop continues from there. Any other character is a syntax error, and the output array size limits the count. The result 12, 7, 255, 3 shows that spaces around the numbers are accepted.
strtol is available in newlib-nano on the STM32, but scanf-family functions pull in much more code. A hand-written cursor parser is small and predictable.Example 8: Sliding-Window Average
Problem: Smooth ADC readings with the average of the last four samples. Two pointers mark the window: front is the newest element, back the oldest; their difference tells how many samples the window holds (growing up to four at the start). Instead of re-adding four values each time, the running sum adds the new sample and subtracts the one that leaves. The spike of 400 at index 3 is spread over the next four outputs: after four samples (100, 110, 90, 400) the average is 175; as it leaves the window the average returns to about 100. The 32-bit sum prevents overflow of the 16-bit samples.
Example 9: Mini Exercises
Exercise 1: What is sizeof(table) in Example 2 and how many words does the loop process?
Exercise 2: Why does while (n--) run exactly n times?
Exercise 3: In Example 3, what makes the loop stop for an odd length?
lo < hi becomes false.Exercise 4: Why does the ring buffer need the counter used?
Exercise 5: What is the type of m[r] for int m[3][4]?
int [4], an array of four ints, which decays to int * in expressions.Exercise 6: When must the copy run backward?
Exercise 7: Why is the sum in Example 8 declared uint32_t?
Key Takeaways from the Pointer Arithmetic Examples in Embedded C
- These pointer arithmetic examples in embedded C show that
*p++combines access and step in one expression. - A pointer type fixes the step: bytes, half-words or words.
- Two pointers moving towards each other process symmetric problems without indices.
- Ring buffers wrap a pointer by comparing it with
buf + CAP. - Overlapping copies must choose the direction; use
memmove. - A running sum with front and back pointers makes a sliding-window filter cheap.
Ready for the quiz?
Next: predict pointer-arithmetic results, find the off-by-one bugs and answer the interview questions.
Need Help or More Information?
Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments