Function Pointer Interview Questions for Embedded C: Quiz and Code Challenges-Free Embedded C Programming Course

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

MODULE 8 · LESSON 8.4 · PRACTICE

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?

2. Which declaration is a pointer to a function taking an int and returning an int?

3. What is int *fp(int);?

4. Which calls are equivalent for a function pointer fp?

5. What typically happens when you call a NULL function pointer on a Cortex-M4?

6. What is the first word of the Cortex-M vector table?

7. At which offset is the SysTick handler (exception 15) stored?

8. Why is bit 0 of every address in the vector table set?

9. What is a callback?

10. Why add a void *ctx parameter to a callback?

11. What is the main advantage of a static const dispatch table over a switch?

12. Which header declares qsort?

13. Is it safe to cast a void (*)(void) to int (*)(int) and call it?

14. How can a function pointer type be given a readable name?

15. A callback is invoked from an ISR. What should it do?

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:

2. Declare op as a pointer to a function taking two ints and returning an int:

3. Write a typedef named cb_t for a pointer to a function taking void * and returning void:

4. The initial stack pointer is stored at table offset 0x

5. The Reset_Handler address is stored at table offset 0x

6. For IRQ number 6 (EXTI0) the vector table offset is 0x

7. A function at 0x08000190 appears in the vector table as 0x0800019

8. A table int (*t[4])(int) holds function pointers

Section 3: Which Feature Fits?

Choose the feature for each need, as asked in function pointer interview questions about design.

NeedYour answerResult
Select a handler by command codedispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Sort by a user-defined keydispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Know which object fired the eventdispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Interrupt entry pointsdispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Swap UART for a test bufferdispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Keep a table out of RAMdispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Avoid a crash on an unset hookdispatch tablecomparison callbackvoid * context argumentvector tablestruct of function pointersstatic const arrayNULL check before the calltypedef
Readable pointer typedispatch 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.

2. You can do arithmetic on function pointers.

3. int (*fp)(int) and int *fp(int) declare the same thing.

4. A function pointer can be stored in an array or a struct.

5. Calling a function through a pointer with the wrong signature is safe if the types are similar.

6. Interrupt handlers are called through addresses stored in the vector table.

7. A static const table of function pointers occupies RAM.

8. A callback with a context pointer can serve several objects.

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> static int inc(int x) { return x + 1; } static int 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); return 0; }

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> static int a(void) { return 1; } static int b(void) { return 2; } static int c(void) { return 3; } 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); return 0; }

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> static void 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(); return 0; }

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> static void apply(int *a, int n, int (*f)(int)) { for (int i = 0; i < n; i++) a[i] = f(a[i]); } static int 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]); return 0; }

What is printed?

Output:
-1 2 -3
neg is applied to each element: -1, 2, -3.
Challenge 5 — Find the bug: Signature mismatch
#include <stdio.h> static int twice(int x) { return 2 * x; } int main(void) { int (*fp)(int, int) = twice; printf(“%d\n”, fp(3, 4)); return 0; }

What does GCC report?

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> static int h(int x) { return x; } int main(void) { int *p = h; /* is this a function pointer? */ printf(“%d\n”, p != NULL); return 0; }

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> typedef int (*binop_t)(int, int); static int 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)); return 0; }
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+
ctx_iq.c
#include <stdio.h> typedef void (*cb_t)(void *ctx); static void fire(cb_t cb, void *ctx) { if (cb) cb(ctx); } static void bump(void *ctx) { int *n = ctx; (*n)++; } int main(void) { int count = 0; fire(bump, &count); fire(bump, &count); fire(NULL, &count); printf(“%d\n”, count); return 0; }
Output (verified by compiling and running)
2

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

ScoreMeaningNext step
90–100%ExcellentModule 8 is complete. Continue to Module 9: pointers.
70–89%GoodRedo Examples 3, 4 and 6 of the examples page.
Below 70%ReviseRe-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.

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *