BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C – Function Pointer with examples

Last Updated: September 24, 2017 by Chaitanya Singh | Filed Under: c-programming

In C programming language, we can have a concept of Pointer to a function known as function pointer in C. In this tutorial, we will learn how to declare a function pointer and how to call a function using this pointer. To understand this concept, you should have the basic knowledge of Functions and Pointers in C.

How to declare a function pointer?

function_return_type(*Pointer_name)(function argument list)

For example:

double  (*p2f)(double, char)

Here double is a return type of function, p2f is name of the function pointer and (double, char) is an argument list of this function. Which means the first argument of this function is of double type and the second argument is char type.

Lets understand this with the help of an example: Here we have a function sum that calculates the sum of two numbers and returns the sum. We have created a pointer f2p that points to this function, we are invoking the function using this function pointer f2p.

int sum (int num1, int num2)
{
    return num1+num2;
}
int main()
{

    /* The following two lines can also be written in a single
     * statement like this: void (*fun_ptr)(int) = &fun;
     */
    int (*f2p) (int, int);
    f2p = sum;
    //Calling function using function pointer
    int op1 = f2p(10, 13);

    //Calling function in normal way using function name
    int op2 = sum(10, 13);

    printf("Output1: Call using function pointer: %d",op1);
    printf("\nOutput2: Call using function name: %d", op2);

    return 0;
}

Output:

Output1: Call using function pointer: 23
Output2: Call using function name: 23

Some points regarding function pointer:
1. As mentioned in the comments, you can declare a function pointer and assign a function to it in a single statement like this:

void (*fun_ptr)(int) = &fun;

2. You can even remove the ampersand from this statement because a function name alone represents the function address. This means the above statement can also be written like this:

void (*fun_ptr)(int) = fun;
❮ PreviousNext ❯

Top Related Articles:

  1. Passing pointer to a function in C with example
  2. C – Pointer to Pointer (Double Pointer) with example
  3. Function call by reference in C Programming
  4. C – loops in C programming with examples
  5. C – switch case statement in C Programming with example

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Nikitha Nischal says

    June 24, 2016 at 9:42 PM

    Instead of

    f2p = sum ;

    It should have been

    f2p = &num ;

    since we are assigning the address of the function num to the pointer variable p2f.

    Am i right? Please clarify.

    Reply
    • tayyar says

      December 13, 2016 at 9:44 PM

      No, the adress operator is not needed, since the name of a function already “is” a pointer to the functions’s adress.

      Reply
    • Yash says

      August 17, 2017 at 2:10 PM

      No because f2p is pointer type whereas sum is function name which itslef is an address so its correct

      Reply
  2. Gunjan Panda says

    February 11, 2017 at 3:09 AM

    In sum function,it should return num1+num2 as sum1 and sum2 are not declared.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap