Pointer Arithmetic Interview Questions for Embedded C: Quiz and Code Challenges
57 questions: multiple choice, write-the-expression blanks, an expression-matching table, true/false, 6 code challenges with real output and 12 detailed interview answers. Treat it as a model for pointer arithmetic interview questions.
57
Questions
Beginner → Advanced
Level
Live score
Auto-graded
Live Score Tracker
0 Correct
0 Wrong
0 Attempted
Answer the auto-graded questions (MCQ, fill-in, true/false) to see your score.
*(p + 2) is h[2] = 0506; b[3] is the high byte of h[1] = 0x0304, which is 03; three uint16_t elements are 6 bytes.
Challenge 4 — Output?: Row pointers
#include <stdio.h>int main(void)
{
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*r)[3] = m + 1;
printf(“%d %d %d\n”, (*r)[0], r[0][2], *(*m + 4));
return0;
}
What is printed?
Output:
4 6 5
r points to row 1 (4, 5, 6): (*r)[0] = 4 and r[0][2] = 6. *m is row 0 as an int pointer, so *(*m + 4) is the fifth element, 5.
Challenge 5 — Find the bug: The loop condition
#include <stdio.h>int main(void)
{
int a[4] = {1, 2, 3, 4};
int s = 0;
for (int i = 0; i <= 4; i++)
s += a[i];
printf(“%d\n”, s);
return0;
}
What does GCC report with -O2 -Wall?
Compiler result:offbyone.c: In function ‘main’:
offbyone.c:7:15: warning: iteration 4 invokes undefined behavior [-Waggressive-loop-optimizations]
7 | s += a[i];
| ~^~~
offbyone.c:6:23: note: within this loop
6 | for (int i = 0; i <= 4; i++)
| ~~^~~~The loop runs for i = 0 to 4, but valid indices are 0 to 3. Use i < 4.
Challenge 6 — Find the bug: Adding two pointers
#include <stdio.h>int main(void)
{
int a[3] = {1, 2, 3};
int *p = a, *q = a + 2;
printf(“%d\n”, *(p + q));
return0;
}
What does GCC report?
Compiler result:addptr.c: In function ‘main’:
addptr.c:6:24: error: invalid operands to binary + (have ‘int *’ and ‘int *’)
6 | printf(“%d\n”, *(p + q));
| ^Adding two pointers is meaningless. Use p + (q - p) or just q.
Section 6: Pointer Arithmetic Interview Questions for Embedded C (with Detailed Answers)
Pointer arithmetic interview questions test scaling, the array and pointer relationship, increment idioms, pointer subtraction and bounds. Click a question for a model answer.
Q1. What is pointer arithmetic?Basic+
The set of valid operations on pointers: adding or subtracting an integer, subtracting two pointers into the same array and comparing pointers. Adding n advances n elements of the pointed-to type, so the compiler multiplies by sizeof(*p). The result must stay inside the array or one past its end.
The language defines the subscript that way. An array name decays to a pointer to its first element, a + i moves i elements and the star reads the element. Because addition is commutative, i[a] works too, but is never used.
Q3. What is the difference between an array and a pointer?Basic+
Array
Pointer
Storage
Holds the elements
Holds one address
sizeof
Whole array
Pointer size
Assignable
No
Yes
Increment
No
Yes
As a parameter
Becomes a pointer
Is a pointer
Address-of
&a has type pointer-to-array
&p is pointer-to-pointer
An array name converts to a pointer to its first element in most expressions, which makes the two look alike.
Q4. Explain *p++, (*p)++, *++p and ++*p.Intermediate+
Expression
Effect
*p++
value is the old *p; p advances
(*p)++
value is the old *p; the object increases by 1
*++p
p advances; value is the new *p
++*p
the object increases by 1; value is the new value
Postfix operators bind tighter than prefix operators, and prefix operators associate right to left.
Q5. What does sizeof return for an array parameter and why?Intermediate+
The size of a pointer. A parameter declared int a[10] is adjusted to int *a, so the function has no knowledge of the original length. Pass the length separately. Inside the caller, where the real array is visible, sizeof a / sizeof a[0] gives the element count.
Q6. What is the one-past-the-end pointer and why does C allow it?Intermediate+
For an array of n elements, a + n may be formed and compared but not dereferenced. It allows the idiomatic loop for (p = a; p != a + n; p++) and the length expression end - begin. Pointers further out, or before the first element, are undefined even if never used, because on some architectures the computation itself can trap or wrap.
Q7. How does a ring buffer use pointer arithmetic?Intermediate+
ring_iq.c
#include <stdio.h>#include <stdint.h>int main(void)
{
uint8_t buf[4];
uint8_t *head = buf;
for (int i = 0; i < 6; i++)
{
*head++ = (uint8_t)i;
if (head == buf + 4) head = buf;
}
printf(“head index %td, buf: %u %u %u %u\n”, head – buf, buf[0], buf[1], buf[2], buf[3]);
return0;
}
Output (verified by compiling and running)
head index 2, buf: 4 5 2 3
Producer and consumer pointers advance with *p++ and wrap by comparing against buf + size; a counter (or one wasted slot) distinguishes full from empty. For power-of-two sizes, indices with a mask (i & (N-1)) are an alternative.
Q8. How do you step through a structure or buffer byte by byte?Intermediate+
Cast the address to const uint8_t * (or unsigned char *): ISO C allows any object to be examined through a character type. Then loop for (i = 0; i < sizeof obj; i++). This is used for checksums, serialisation and hex dumps; it exposes padding bytes and the byte order, so do not use it to define a wire format.
Q9. Why can (uint32_t *)buf cause problems when buf is a byte array?Advanced+
The Cortex-M4 can do unaligned word loads for normal LDR/STR, but not for LDM, LDRD or exclusive accesses, and the compiler may assume the alignment of the pointed-to type, generating instructions that fault when the address is odd. In addition, strict aliasing says that reading a uint8_t array through a uint32_t pointer is undefined. Use memcpy into a local variable, or build the value with shifts (Lesson 9.5).
Q10. What is the difference between int *p[4] and int (*p)[4]?Advanced+
The first is an array of 4 pointers to int (each can point to a different buffer). The second is a single pointer to an array of 4 ints (used for row pointers of a matrix). Reading right to left: p[4] binds first, so it is an array of pointers; the parentheses force the star first, so it is a pointer to an array.
Q11. How can pointer arithmetic go wrong with signed and unsigned types?Advanced+
Mixing size_t (unsigned) and int in an expression converts the int to unsigned: p + n - 1 with n = 0 and unsigned n underflows to a huge number, and i >= 0 is always true for an unsigned counter, giving an endless loop when counting down. Use ptrdiff_t for differences and signed counters for downward loops, or the idiom while (i--).
Q12. How would you find the length of a zero-terminated string without strlen?Advanced+
Walk a copy of the pointer to the terminator and subtract the start. The result is the number of characters excluding the zero. The loop reads one byte per iteration; an optimised library version reads words, which requires care about alignment and reading past the end.
Study Plan: Using Your Score on These Pointer Arithmetic Interview Questions
Score
Meaning
Next step
90–100%
Excellent
Continue to Lesson 9.3: pointer to pointer, void pointers and NULL.
70–89%
Good
Redo Examples 1, 4 and 6 of the examples page.
Below 70%
Revise
Re-read theory sections 1, 2, 3 and 6, then retake the quiz.
Habit to build: Write the byte offset next to every pointer expression you read: element index times sizeof the type.
Lesson 9.2 complete!
Next up: pointer to pointer, void pointers, NULL and dangling pointers.
1 Comment