Function Pointer Interview Questions for Embedded C: Quiz and Code Challenges
57 questions: multiple choice, write-the-declaration blanks, a feature-matching table, true/false, 6 code challenges with real output and 12 detailed interview answers. Treat it as a model for function pointer 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.
Section 1: Multiple Choice Questions — Function Pointers
These function pointer interview questions appear in nearly every embedded C screening round. Click an answer; explanations always appear.
1. What does a function pointer store?
It is an address in Flash on the STM32F407.
2. Which declaration is a pointer to a function taking an int and returning an int?
The parentheses around *fp matter.
3. What is int *fp(int);?
The * binds to the return type.
4. Which calls are equivalent for a function pointer fp?
Both forms are valid and identical.
5. What typically happens when you call a NULL function pointer on a Cortex-M4?
The branch goes to address 0 with the Thumb bit clear.
6. What is the first word of the Cortex-M vector table?
Word 1 (offset 4) is Reset_Handler.
7. At which offset is the SysTick handler (exception 15) stored?
4 × 15 = 0x3C.
8. Why is bit 0 of every address in the vector table set?
Clearing it would raise a UsageFault.
9. What is a callback?
The driver calls it when an event occurs.
10. Why add a void *ctx parameter to a callback?
The registrant chooses it; the driver passes it back.
11. What is the main advantage of a static const dispatch table over a switch?
Adding a command is a one-line change.
12. Which header declares qsort?
qsort takes a comparison callback.
13. Is it safe to cast a void (*)(void) to int (*)(int) and call it?
The call must match the real signature.
14. How can a function pointer type be given a readable name?
typedef int (*binop_t)(int, int);
15. A callback is invoked from an ISR. What should it do?
Long work belongs in the main loop.
Section 2: Write or Evaluate (Fill in the Blanks)
Type the declaration for the first three items (spacing is flexible) and a short value for the rest.
1. Declare fp as a pointer to a function with no parameters returning void:
The parentheses around *fp are needed.
2. Declare op as a pointer to a function taking two ints and returning an int:
Same shape as the prototype.
3. Write a typedef named cb_t for a pointer to a function taking void * and returning void:
The typedef name sits where the variable name would be.
4. The initial stack pointer is stored at table offset 0x
Word 0.
5. The Reset_Handler address is stored at table offset 0x
Word 1.
6. For IRQ number 6 (EXTI0) the vector table offset is 0x
4 × (16 + 6) = 0x58.
7. A function at 0x08000190 appears in the vector table as 0x0800019
The Thumb bit is set.
8. A table int (*t[4])(int) holds function pointers
Four entries.
Section 3: Which Feature Fits?
Choose the feature for each need, as asked in function pointer interview questions about design.
Need
Your answer
Result
Select a handler by command code
dispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Sort by a user-defined key
dispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Know which object fired the event
dispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Interrupt entry points
dispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Swap UART for a test buffer
dispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Keep a table out of RAM
dispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Avoid a crash on an unset hook
dispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Readable pointer type
dispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Section 4: True or False
Statements typical of function pointer interview questions.
1. A function name without parentheses evaluates to its address.
Function-to-pointer conversion.
2. You can do arithmetic on function pointers.
Not allowed in C.
3. int (*fp)(int) and int *fp(int) declare the same thing.
The second is a function returning int *.
4. A function pointer can be stored in an array or a struct.
This gives dispatch tables and interfaces.
5. Calling a function through a pointer with the wrong signature is safe if the types are similar.
Undefined behavior.
6. Interrupt handlers are called through addresses stored in the vector table.
The core reads the table entry.
7. A static const table of function pointers occupies RAM.
It is placed in Flash.
8. A callback with a context pointer can serve several objects.
The context selects the object.
Section 5: Code Snippet Challenges
Trace each program on paper first; the real output follows. These challenges are the code-reading half of function pointer interview questions.
Challenge 1 — Output?: Reassigning a function pointer
#include <stdio.h>staticint inc(int x) { return x + 1; }
staticint dbl(int x) { return x * 2; }
int main(void)
{
int (*f)(int) = inc;
int a = f(5);
f = dbl;
int b = f(a);
printf(“%d %d\n”, a, b);
return0;
}
What is printed?
Output:
6 12
f(5) with inc gives 6; after f = dbl, f(6) gives 12.
Challenge 2 — Output?: An array of function pointers
#include <stdio.h>staticint a(void) { return1; }
staticint b(void) { return2; }
staticint c(void) { return3; }
int main(void)
{
int (*t[])(void) = { a, b, c };
int sum = 0;
for (int i = 2; i >= 0; i–)
sum = sum * 10 + t[i]();
printf(“%d\n”, sum);
return0;
}
What is printed?
Output:
321
The loop calls c, b, a in that order: 0×10+3 = 3, 3×10+2 = 32, 32×10+1 = 321.
Challenge 3 — Output?: NULL check
#include <stdio.h>staticvoid hi(void) { printf(“hi\n”); }
int main(void)
{
void (*fp)(void) = NULL;
if (fp) fp();
else printf(“no handler\n”);
fp = hi;
if (fp) fp();
return0;
}
What is printed?
Output:
no handler
hi
The first test finds NULL and prints “no handler”; after assignment the call runs hi().
Challenge 4 — Output?: A callback applied to an array
#include <stdio.h>staticvoid apply(int *a, int n, int (*f)(int))
{
for (int i = 0; i < n; i++) a[i] = f(a[i]);
}
staticint neg(int x) { return -x; }
int main(void)
{
int v[3] = {1, –2, 3};
apply(v, 3, neg);
printf(“%d %d %d\n”, v[0], v[1], v[2]);
return0;
}
Compiler result:sigmismatch.c: In function ‘main’:
sigmismatch.c:5:27: warning: initialization of ‘int (*)(int, int)’ from incompatible pointer type ‘int (*)(int)’ [-Wincompatible-pointer-types]
5 | int (*fp)(int, int) = twice;
| ^~~~~The pointer type takes two arguments but twice takes one. Calling through it would be undefined behavior; fix the pointer type or the function.
Challenge 6 — Find the bug: Assigning a function to an int pointer
#include <stdio.h>staticint h(int x) { return x; }
int main(void)
{
int *p = h; /* is this a function pointer? */
printf(“%d\n”, p != NULL);
return0;
}
What does GCC report?
Compiler result:wrongptr.c: In function ‘main’:
wrongptr.c:5:14: warning: initialization of ‘int *’ from incompatible pointer type ‘int (*)(int)’ [-Wincompatible-pointer-types]
5 | int *p = h; /* is this a function pointer? */
| ^A function address is not an int *. Declare int (*p)(int) = h;.
Section 6: Function Pointer Interview Questions for Embedded C (with Detailed Answers)
Function pointer interview questions test declarations, callbacks, dispatch tables, the vector table and the Thumb bit. Click a question for a model answer.
Q1. What is a function pointer and when would you use one?Basic+
A function pointer holds the address of a function and lets you call it indirectly. Typical uses in embedded C: callbacks that a driver invokes when an event occurs, dispatch tables replacing switch statements, state machines, driver interfaces (a struct of function pointers), and the interrupt vector table.
Q2. How do you declare and call a function pointer?Basic+
fp_iq.c
#include <stdio.h>typedefint (*binop_t)(int, int);
staticint add(int a, int b) { return a + b; }
int main(void)
{
int (*fp)(int, int) = add;
binop_t g = add;
printf(“%d %d\n”, fp(2, 3), (*g)(4, 5));
return0;
}
Output (verified by compiling and running)
5 9
Replace the function name in the prototype by (*name). Both fp(args) and (*fp)(args) call the target.
Q3. What is the difference between int (*fp)(int) and int *fp(int)?Basic+
The first declares fp as a pointer to a function. The second declares a function named fp that returns a pointer to int. The parentheses around *fp make the difference.
Q4. Explain the vector table and how an interrupt reaches its handler.Intermediate+
At the start of Flash (aliased at address 0 after reset) the vector table holds the initial stack pointer, the reset handler address and the addresses of the exception and interrupt handlers, each with the Thumb bit set. When an interrupt with IRQ number n arrives, the core stacks eight registers, reads the 32-bit entry at offset 4×(16+n), loads it into the program counter and runs the handler. The table can be relocated with the VTOR register, which a bootloader or RTOS uses.
Q5. What is the Thumb bit and why does it matter for function pointers?Intermediate+
Bit 0 of a branch target selects the instruction set. The Cortex-M4 supports only Thumb, so the bit must be 1. The compiler and linker set it automatically in every function pointer, so a function placed at 0x08000190 has the pointer value 0x08000191. If you build a pointer from an integer (a bootloader jump, a callback table in a register) you must set bit 0 yourself, otherwise the core raises a UsageFault.
Q6. How do you implement a callback with user data?Intermediate+
The registrant passes a void * that the library stores and hands back unchanged; the callback casts it to the real type. A NULL callback is simply ignored.
Q7. How can function pointers replace a large switch statement?Intermediate+
Put the handlers in an array indexed by the selector, or in a table of structs holding a key and a handler. The dispatch code is a bounds check plus one call. Benefits: data-driven, easy to extend, can live in Flash as static const, and the table can carry extra fields such as names and help text. A switch on dense values may compile to a jump table anyway, so choose by maintainability, not by speed.
Q8. How does a struct of function pointers implement an interface in C?Intermediate+
The struct lists the operations (open, read, write…). Each driver provides one constant instance filled with its own functions. Client code receives a pointer to the struct and calls through it, so it works with any driver, including a mock for unit tests. This is the basis of many HAL, file-system and USB stacks and the C way of writing polymorphic code.
Q9. What are the risks of function pointers in safety-critical code?Advanced+
They hide the control flow from static analysis and stack-depth tools, an unset or corrupted pointer causes a jump to arbitrary code, and an unchecked table index calls arbitrary addresses. Mitigations: const tables in Flash, bounds checks, NULL checks, a single typedef per signature, no casts between function pointer types, and documentation of every callback’s calling context.
Q10. How does a bootloader jump to an application?Advanced+
The bootloader reads word 0 (stack pointer) and word 1 (reset handler) from the application’s vector table, validates both (SP in SRAM, Thumb bit set, address within the application region), disables interrupts and peripherals, sets VTOR to the application table, loads MSP with the new stack pointer (__set_MSP), and finally calls the reset address through a function pointer of type void (*)(void). It never returns.
Q11. Why is calling a function pointer through a mismatched type undefined?Advanced+
The caller places arguments in registers r0–r3 and expects the result in r0 according to the type it sees; the callee reads its own idea of the arguments. With different parameter counts or sizes, values are lost or garbage, and with a different return type the result is wrong. The compiler may also optimise on the assumption that types match. Use wrapper functions with the correct signature instead of casts.
Q12. What is the difference between calling a function directly and through a pointer, in machine code?Advanced+
A direct call compiles to bl function with the target encoded in the instruction. A call through a pointer loads the address into a register and executes blx rN. The indirect call prevents inlining and some optimisations, costs a load and a register, and has a data-dependent target that the CPU’s branch predictor and static analysers must handle; on a Cortex-M4 the difference is only a few cycles.
Study Plan: Using Your Score on These Function Pointer Interview Questions
Score
Meaning
Next step
90–100%
Excellent
Module 8 is complete. Continue to Module 9: pointers.
70–89%
Good
Redo Examples 3, 4 and 6 of the examples page.
Below 70%
Revise
Re-read theory sections 2, 4, 5 and 7, then retake the quiz.
Habit to build: Give every callback type a typedef and check each optional pointer for NULL before the call.
Module 8 complete!
Next up: Module 9, pointers: addresses, pointer arithmetic, arrays and pointers to pointers.
2 Comments