Declaring Function Pointers
Pointers to functions.
Declaring Function Pointers is a free C Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.
Functions Have Addresses
Just like data, every function lives at an address in memory. A function pointer stores that address so you can call the function indirectly.
#include <stdio.h>
void greet(void) { printf("hello\n"); }
int main(void) {
void (*fp)(void) = greet;
fp();
return 0;
}Reading the Declaration
The declaration int (*fp)(int) means: fp is a pointer to a function taking an int and returning an int.
The parentheses around *fp are required.
#include <stdio.h>
int square(int x) { return x * x; }
int main(void) {
int (*fp)(int) = square;
printf("%d\n", fp(5));
return 0;
}Assigning a Function
A function name decays to its address, so fp = square and fp = &square are equivalent.
#include <stdio.h>
int dbl(int x) { return x * 2; }
int main(void) {
int (*fp)(int);
fp = &dbl;
printf("%d\n", fp(7));
return 0;
}Calling Through the Pointer
You can call through a function pointer with fp(args) or the explicit (*fp)(args). Both do the same thing.
#include <stdio.h>
int add1(int x) { return x + 1; }
int main(void) {
int (*fp)(int) = add1;
printf("%d\n", fp(10));
printf("%d\n", (*fp)(10));
return 0;
}The Signature Must Match
A function pointer's type includes its return type and parameter types. You can only assign a function whose signature matches exactly.
#include <stdio.h>
double half(double x) { return x / 2.0; }
int main(void) {
double (*fp)(double) = half;
printf("%f\n", fp(9.0));
return 0;
}Reassigning at Runtime
A function pointer is a variable, so you can point it at different functions while the program runs.
#include <stdio.h>
int inc(int x) { return x + 1; }
int dec(int x) { return x - 1; }
int main(void) {
int (*op)(int) = inc;
printf("%d\n", op(5));
op = dec;
printf("%d\n", op(5));
return 0;
}Choosing by Condition
Select which function to call based on a runtime value, then invoke through one pointer.
#include <stdio.h>
int plus(int a) { return a + 10; }
int minus(int a) { return a - 10; }
int main(void) {
int up = 1;
int (*op)(int) = up ? plus : minus;
printf("%d\n", op(100));
return 0;
}typedef for Readability
Function pointer syntax is verbose. A typedef gives the type a name, making declarations clearer.
#include <stdio.h>
typedef int (*IntOp)(int);
int triple(int x) { return x * 3; }
int main(void) {
IntOp op = triple;
printf("%d\n", op(4));
return 0;
}Two-Argument Functions
Function pointers can describe any signature. Here is one for a binary integer operation.
#include <stdio.h>
int sum(int a, int b) { return a + b; }
int main(void) {
int (*op)(int, int) = sum;
printf("%d\n", op(3, 4));
return 0;
}A Null Function Pointer
Like data pointers, a function pointer can be NULL. Always check before calling one that may be unset.
#include <stdio.h>
int twice(int x) { return x * 2; }
int main(void) {
int (*op)(int) = NULL;
if (op == NULL) op = twice;
if (op) printf("%d\n", op(8));
return 0;
}Printing the Address
You can inspect the stored address. Different functions have different addresses.
#include <stdio.h>
int f(int x) { return x; }
int main(void) {
int (*fp)(int) = f;
printf("function pointer is %s\n", fp ? "set" : "null");
return 0;
}Quick Check
Test your understanding of function pointer declarations.
Recap
You learned to declare and use function pointers:
- Syntax:
ret (*name)(params)with required parentheses. - Assign with the function name (it decays to an address).
- Call with
fp(args)or(*fp)(args). - The signature must match;
typedefimproves readability.
Learn C with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 39
- Lessons
- 144
Frequently Asked Questions
Is the “Declaring Function Pointers” lesson free?
Yes — the full text of “Declaring Function Pointers” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 4 lessons in total.
What will I learn in “Declaring Function Pointers”?
Pointers to functions. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C Academy?
No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Declaring Function Pointers” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C Academy lesson?
Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Declaring Function Pointers
- Passing Functions
- qsort with Comparators
- Function Pointer Tables