Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • C
  • C Basics
  • C Data Types
  • C Operators
  • C Input and Output
  • C Control Flow
  • C Functions
  • C Arrays
  • C Strings
  • C Pointers
  • C Preprocessors
  • C File Handling
  • C Programs
  • C Cheatsheet
  • C Interview Questions
  • C MCQ
  • C++
Open In App
Next Article:
How to Declare a Pointer to a Function?
Next article icon

Function Pointer in C

Last Updated : 01 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves differently based on the context).

Let's take a look at an example:

C
#include <stdio.h>  int add(int a, int b) {     return a + b; }  int main() {        // Declare a function pointer that matches   	// the signature of add() fuction     int (*fptr)(int, int);      // Assign to add()     fptr = &add;      // Call the function via ptr     printf("%d", fptr(10, 5));     return 0; } 

Output
15

Explanation: In this program, we define a function add(), assigns its address to a function pointer fptr, and invokes the function through the pointer to print the sum of two integers.

Function Pointer Declaration

Function pointers are declared according to the signature of the function they will be pointing to. Below is the generic syntax of function pointer declaration:

C
return_type (*pointer_name)(parameter_types); 
  • return_type: The type of the value that the function returns.
  • parameter_types: The types of the parameters the function takes.
  • pointer_name: The name of the function pointer.

The parenthesis around the pointer_name is necessary, otherwise, it will be treated as a function declaration with the return type of return_type* and name pointer_name.

The type of the function is decided by its return type, number and type of parameters. So, the function pointer should be declared in such a way that it matches the signature of the function it later points to. For example, in the above code, the function pointer was declared as:

C
int (*fpr)(int, int); 

which matches the signature of the add() function that it later points to.

Initialization

A function pointer is then initialized by assigning the address of the function.

C
pointer_name = &function_name 

We can also skip the address of operator as function name itself behaves like a constant function pointer.

C
pointer_name = function_name; 

It is compulsory to assign the function with similar signature as specified in the pointer declaration. Otherwise, the compiler may show type mismatch error.

Properties of Function Pointer

Function pointer points to the code instead of the data so there are some restrictions on the function pointers as compared to other pointers. Following are some important properties of function pointer:

  • Points to the memory address of a function in the code segment.
  • Requires the exact function signature (return type and parameter list).
  • Can point to different functions with matching signatures.
  • Cannot perform arithmetic operations like increment or decrement.
  • Supports array-like functionality for tables of function pointers.

Applications with Examples

The following programs lists some common applications of function pointers along with code examples:

Function Pointer as Arguments (Callbacks)

One of the most useful applications of function pointers is passing functions as arguments to other functions. This allows you to specify which function to call at runtime.

C
#include <stdio.h>  // A simple addition function int add(int a, int b) {     return a + b; }  // A simple subtraction function int subtract(int a, int b) {     return a - b; }  void calc(int a, int b, int (*op)(int, int)) {     printf("%d\n", op(a, b)); }  int main() {        // Passing different      // functions to 'calc'     calc(10, 5, add);   	calc(10, 5, subtract);     return 0; } 

Output
15 5 

Explanation: The calc function accepts a function pointer operation that is used to perform a specific operation (like addition or subtraction) on the two integers a and b. By passing the add or subtract function to calc, the correct function is executed dynamically.

Emulate Member Functions in Structure

We can create a data member inside structure, but we cannot define a function inside it. But we can define function pointers which in turn can be used to call the assigned functions.

C
#include <stdio.h>  // Define the Rectangle struct that contains pointers // to functions as member functions typedef struct Rect {     int w, h;     void (*set)(struct Rect*, int, int);     int (*area)(struct Rect*);     void (*show)(struct Rect*); } Rect;  // Function to find the area int area(Rect* r) {     return r->w * r->h; }  // Function to print the dimensions void show(Rect* r) {     printf("Rectangle's Width: %d, "            "Height: %d\n", r->w, r->h); }  // Function to set width  // and height (setter) void set(Rect* r, int w, int h) {     r->w = w;     r->h = h; }  // Initializer/constructor  // for Rectangle void constructRect(Rect* r) {     r->w = 0;     r->h = 0;     r->set = set;     r->area = area;     r->show = show; }  int main() {     // Create a Rectangle object     Rect r;     constructRect(&r);      // Use r as a Rectangle     r.set(&r, 10, 5);     r.show(&r);     printf("Rectangle Area: %d", r.area(&r));     return 0; } 

Output
Rectangle's Width: 10, Height: 5 Rectangle Area: 50

Array of Function Pointers

You can also use function pointers in arrays to implement a set of functions dynamically.

C
#include <stdio.h>  // Function declarations int add(int a, int b) {     return a + b; } int sub(int a, int b) {     return a - b; } int mul(int a, int b) {     return a * b; } int divd(int a, int b) {     return a / b; }  int main() {          // Declare an array of function pointers     int (*farr[])(int, int) = {add, sub, mul, divd};     int x = 10, y = 5;      // Dynamically call functions using the array     printf("Sum: %d\n", farr[0](x, y));      printf("Difference: %d\n", farr[1](x, y));     printf("Product: %d", farr[2](x, y));      return 0; } 

Output
Sum: 15 Difference: 5 Product: 50

Next Article
How to Declare a Pointer to a Function?

A

Abhay Rathi
Improve
Article Tags :
  • C Language
  • cpp-pointer
  • CPP-Functions
  • C-Pointers

Similar Reads

    C Pointers
    A pointer is a variable that stores the memory address of another variable. Instead of holding a direct value, it has the address where the value is stored in memory. This allows us to manipulate the data stored at a specific memory location without actually using its variable. It is the backbone of
    9 min read
    Pointer Arithmetics in C with Examples
    Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn't store any value. Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer
    10 min read
    Applications of Pointers in C
    Pointers in C are variables that are used to store the memory address of another variable. Pointers allow us to efficiently manage the memory and hence optimize our program. In this article, we will discuss some of the major applications of pointers in C. Prerequisite: Pointers in C. C Pointers Appl
    4 min read
    Passing Pointers to Functions in C
    Prerequisites: Pointers in CFunctions in C Passing the pointers to the function means the memory location of the variables is passed to the parameters in the function, and then the operations are performed. The function definition accepts these addresses using pointers, addresses are stored using po
    2 min read
    C - Pointer to Pointer (Double Pointer)
    In C, double pointers are those pointers which stores the address of another pointer. The first pointer is used to store the address of the variable, and the second pointer is used to store the address of the first pointer. That is why they are also known as a pointer to pointer.Let's take a look at
    5 min read
    Chain of Pointers in C with Examples
    Prerequisite: Pointers in C, Double Pointer (Pointer to Pointer) in CA pointer is used to point to a memory location of a variable. A pointer stores the address of a variable.Similarly, a chain of pointers is when there are multiple levels of pointers. Simplifying, a pointer points to address of a v
    5 min read
    Function Pointer in C
    In C, a function pointer is a type of pointer that stores the address of a function, allowing functions to be passed as arguments and invoked dynamically. It is useful in techniques such as callback functions, event-driven programs, and polymorphism (a concept where a function or operator behaves di
    6 min read
    How to Declare a Pointer to a Function?
    A pointer to a function is similar to a pointer to a variable. However, instead of pointing to a variable, it points to the address of a function. This allows the function to be called indirectly, which is useful in situations like callback functions or event-driven programming.In this article, we w
    2 min read
    Pointer to an Array | Array Pointer
    A pointer to an array is a pointer that points to the whole array instead of the first element of the array. It considers the whole array as a single unit instead of it being a collection of given elements.Example:C #include<stdio.h> int main() { int arr[5] = { 1, 2, 3, 4, 5 }; int *ptr = arr;
    5 min read
    Difference between constant pointer, pointers to constant, and constant pointers to constants
    In this article, we will discuss the differences between constant pointer, pointers to constant & constant pointers to constants. Pointers are the variables that hold the address of some other variables, constants, or functions. There are several ways to qualify pointers using const. Pointers to
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences