Skip to content
Learn Netverks

Lesson

Step 21/36 58% through track

function-pointers

Function pointers

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
advanced

This lesson

This lesson teaches Function pointers: the syntax, patterns, and safety habits you need before advancing in C.

Pointers and dynamic allocation are the heart of C interviews—segfaults and leaks come from misunderstanding ownership.

You will apply Function pointers in contexts like: Custom allocators, linked structures, and hand-off APIs in systems libraries.

Write C in main.c with int main(), click Run on server—the dev runner compiles with cc/gcc -std=c11 and runs the binary; read stderr for compile and linker errors (LEARNING_RUNNER_ENABLED=true). Also never return addresses of local variables from functions.

When pointers, structs, and basic control flow from intermediate lessons are familiar.

Functions have addresses too. A function pointer stores that address—enabling callbacks, dispatch tables, and plugin-style APIs.

Syntax

int add(int a, int b) { return a + b; }

int (*op)(int, int) = add;
int result = op(2, 3);

Read declaration inside-out: op is a pointer to function taking two ints and returning int.

typedef for clarity

typedef int (*BinaryOp)(int, int);
BinaryOp op = add;

Important interview questions and answers

  1. Q: Why function pointers?
    A: Runtime polymorphism in C—sort comparators (qsort), event handlers, driver dispatch tables.
  2. Q: NULL function pointer call?
    A: Undefined behavior—guard before invoke.

Self-check

  1. How do you call through a function pointer named op?
  2. What stdlib function uses a comparison function pointer?

Tip: Use typedef to name function pointer types—callbacks in qsort and event handlers become readable.

Interview prep

Why function pointers?

Runtime dispatch—sort comparators, callbacks, plugin APIs, and state machines.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Callback pattern?
  • qsort example?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump