Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Scope C Function Declaration C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Nested Structures C Structs & Pointers C Unions C typedef

C Enums

C Enums

C Memory

C Memory Management

C Errors

C Errors C Debugging C NULL C Error Handling C Input Validation

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers

C Projects

C Projects

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Compiler C Syllabus C Study Plan C Certificate

C Function Pointer


Function Pointer

A function pointer is like a normal pointer, but instead of pointing to a variable, it points to a function.

This means it stores the address of a function, allowing you to call that function using the pointer.

Function pointers let you decide which function to run while the program is running, or when you want to pass a function as an argument to another function.

Think of it like saving a phone number - the pointer knows where the function lives in memory, so you can "call" it later.


Declaring a Function Pointer

The general syntax for declaring a function pointer is:

returnType (*pointerName)(parameterType1, parameterType2, ...);

For example:

int (*ptr)(int, int);

This means ptr is a pointer to a function that takes two int values and returns an int.


Assigning a Function to a Pointer

You can assign a function to a pointer in two ways:

ptr = add;
ptr = &add;

Both are the same, because the function's name already represents its address in memory.


Calling a Function Through a Pointer

Once the pointer is assigned, you can call the function in two ways:

ptr(5, 3);
(*ptr)(5, 3);

Both are valid and do the same thing.


Function Pointer Example

Now that you know how to declare and assign a function pointer, let's see a complete example:

Example

A pointer to a function that adds two numbers:

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

int main() {
  int (*ptr)(int, int) = add;
  int result = ptr(5, 3);
  printf("Result: %d", result);
  return 0;
}

Try it Yourself »

Here:

  • ptr is a pointer to the function add().
  • ptr(5, 3) calls the function using the pointer.

This works exactly the same as calling add(5, 3).

Note: A function name itself points to the start of its code in memory. That means a function name already acts like a pointer! Declaring a function pointer just gives you a variable that can hold that address - so you can change it or pass it around.


Passing a Function as an Argument

Function pointers can be passed to other functions - this is called a callback.

It allows a function to call another function that you provide as input.

Example

Passing a function pointer to another function:

void greetMorning() { printf("Good morning!\n"); }
void greetEvening() { printf("Good evening!\n"); }

void greet(void (*func)()) {
  func();
}

int main() {
  greet(greetMorning);
  greet(greetEvening);
  return 0;
}

Try it Yourself »

Here, greet() takes another function as a parameter and calls it. This is commonly used in event-driven programs or libraries that need user-provided callback functions.


Function Pointer Array

You can also store several function pointers in an array, so you can choose which function to run while the program is running.

This example runs three different functions using an array of function pointers:

Example

void add() { printf("Add\n"); }
void subtract() { printf("Subtract\n"); }
void multiply() { printf("Multiply\n"); }

int main() {
  void (*operations[3])() = { add, subtract, multiply };
  for (int i = 0; i < 3; i++) {
    operations[i]();
  }
  return 0;
}

Try it Yourself »

This is often used for simple menus, command lists, or calculators - anywhere you want to call different functions based on user input.

Example

A simple calculator using an array of function pointers:

void add(int a, int b) { printf("Result: %d\n", a + b); }
void subtract(int a, int b) { printf("Result: %d\n", a - b); }
void multiply(int a, int b) { printf("Result: %d\n", a * b); }

int main() {
  int choice, x = 10, y = 5;

  void (*operations[3])(int, int) = { add, subtract, multiply };

  printf("x = %d, y = %d\n\n", x, y);
  printf("Choose an operation:\n");
  printf("0: Add\n1: Subtract\n2: Multiply\n");
  scanf("%d", &choice);

  if (choice >= 0 && choice < 3) {
    operations[choice](x, y);
  } else {
    printf("Invalid choice!\n");
  }

  return 0;
}

Run Example »

In this program:

  • All three math functions (add(), subtract(), and multiply()) are stored in an array.
  • The user picks which one to run by entering a number.
  • The correct function is then called using the pointer array.

This is a simple example of how function pointer arrays make programs more flexible and reusable.


Function Pointer vs Normal Function

Normal Function Function Pointer
Called directly by its name Called using a pointer
The function is decided before the program runs You can choose which function to call while the program is running
Good for simple code Good for flexible and reusable code

Summary

  • A function pointer stores the address of a function.
  • You can declare, assign, and call functions through it.
  • It allows passing functions as arguments to other functions.
  • Useful for callbacks, menus, and flexible program design.


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.