C Function Pointers

Intermediate
8 min read
Updated: Nov 2026
2 examples

What Is Function Pointers?

A function pointer holds the address of a function. Declare it as return_type (*name)(parameter_types). Use it to choose behavior at run time, to build dispatch tables, and to pass callbacks such as the comparison function for qsort.

Remember
int (*op)(int, int);       /* pointer to function(int,int)->int */
op = add;                  /* function name decays to address */
int r = op(3, 4);          /* or (*op)(3, 4) */
typedef int (*binop)(int, int);

Cheat Sheet

Keep these facts handy while you work with Function Pointers in C.

TopicDetail
Declareret (*name)(params)
Assignfp = function_name;
Callfp(args)
Callbacksqsort, bsearch, signal handlers

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: void Pointers.

Example 1 — A dispatch table of operations

Pick an operation by index.

C
#include <stdio.h>

static int add(int a, int b) { return a + b; }
static int sub(int a, int b) { return a - b; }
static int mul(int a, int b) { return a * b; }

int main(void) {
    int (*ops[])(int, int) = {add, sub, mul};
    const char *names[] = {"add", "sub", "mul"};

    for (int i = 0; i < 3; i++) {
        printf("%s(12, 4) = %d\n", names[i], ops[i](12, 4));
    }
    return 0;
}

How It Works

  1. ops is an array of function pointers.
  2. Each element is called like a normal function.
  3. Adding a new operation only needs a new table entry.

Example 2 — A comparison callback for qsort

The library calls your function to order elements.

C
#include <stdio.h>
#include <stdlib.h>

static int cmp_int(const void *a, const void *b) {
    int x = *(const int *)a;
    int y = *(const int *)b;
    return (x > y) - (x < y);
}

int main(void) {
    int v[] = {5, 2, 9, 1};
    qsort(v, 4, sizeof v[0], cmp_int);
    for (int i = 0; i < 4; i++) printf("%d ", v[i]);
    printf("\n");
    return 0;
}

How It Works

  1. qsort receives cmp_int as a function pointer.
  2. The callback returns negative, zero, or positive.
  3. (x > y) - (x < y) avoids overflow that x - y could cause.

Edge Cases & Pitfalls

Check these before relying on Function Pointers in real programs.

paren

Missing parentheses

int *f(int) declares a function returning int *. Write int (*f)(int) for a pointer.

sig

Mismatched signature

Calling through a pointer of the wrong type is undefined. Use a typedef to keep signatures consistent.

Key Takeaways

  • Declare: ret (*fp)(params).
  • Table: arrays of function pointers.
  • Callbacks: pass behavior as data.

One line: A function pointer holds the address of a function.

Frequently Asked Questions

Write the return type, then (*name), then the parameter types: int (*fp)(int, int). Assign it a function name with a matching signature.
Callbacks let a library call your code, for example qsort calling a comparison function or a signal handler responding to an event.
Continue with "C void Pointers" next. Hold the address of any object with void *, then cast to use it. The C Pointers hub lists every topic in order.

Next: void Pointers

Hold the address of any object with void *, then cast to use it.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful