How to create a typedef for function pointers

93,353

Solution 1

Your question isn't clear, but I think you might want something like this:

int foo(int i){ return i + 1;}

typedef int (*g)(int);  // Declare typedef

g func = &foo;          // Define function-pointer variable, and initialise

int hvar = func(3);     // Call function through pointer

Solution 2

You are right. The function pointer can be conveniently used to point to the different functions of the same return type and taking same number of arguments. The argument types should match the declaration of the function pointer arguments.

In your case you could define your function pointer g as:

typedef int (*g)(int); // typedef of the function pointer.

g is a function pointer for the function returning int value and taking one int argument.

The usage of function pointer could be illustrated by a simple program below:

#include<stdio.h>

typedef int (*pointer_to_function)(int first_parameter_of_type_int, int second_parameter_of_type_int);

int my_function_returning_int_and_taking_two_int_arguments(int par1, int par2)
{
    int result = par1 + par2;
    return result;
}

int my_mul_function(int par1, int par2)
{
    int result = par1 * par2;
    return result;
}

int main()
{
    int res; // returning result will be here
    pointer_to_function my_fun_pointer; // declare function pointer variable;

    my_fun_pointer = my_function_returning_int_and_taking_two_int_arguments; // function pointer points to `my_function_returning_int_and_taking_two_int_arguments` function
    res = my_fun_pointer(2,3);       // Call function through pointer
    printf(" result of `my_function_returning_int_and_taking_two_int_arguments` = %d \n", res);


    my_fun_pointer = my_mul_function; // now function pointer points to another function: `my_mul_function` 
    res = my_fun_pointer(2,3);       // Call function through pointer
    printf(" result of `my_mul_function` = %d \n", res);

   return 0;
}

OUTPUT:

result of `my_function_returning_int_and_taking_two_int_arguments` = 5 
result of `my_mul_function` = 6 
Share:
93,353
SetSlapShot
Author by

SetSlapShot

"We work in one of few industries where we refer to our customers as users" ~Edward Tufte

Updated on December 10, 2020

Comments

  • SetSlapShot
    SetSlapShot over 3 years

    I think it would be easier to use function pointers if I created a typedef for a function pointer, but I seem to be getting myself tripped up on some syntax or usage or something about typedef for function pointers, and I could use some help.

    I've got

    int foo(int i){ return i + 1;}
    typedef <???> g;
    int hvar;
    hvar = g(3)
    

    That's basically what I'm trying to accomplish I'm a rather new C programmer and this is throwing me too much. What replaces <???> ?

  • Jon Purdy
    Jon Purdy about 8 years
    It’s also possible to typedef the function type, which is somewhat uncommon, but may be clearer for illustration: typedef int g(int); g foo; g *func = &foo;. This is useful for declaring (unfortunately, not defining) multiple functions with the same signature.
  • M.M
    M.M about 8 years
    @JonPurdy suggest posting that as an answer
  • Morty
    Morty over 7 years
    How is this related to the question?