programing

How to create a typedef for function pointers

newstyles 2023. 7. 28. 21:52

How to create a typedef for function pointers

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 <???> ?

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

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 

The original way of writing the function returning function pointer is

int (* call(void) ) (int,int);

Here call is a function which takes nothing but returns a function pointer which takes 2 arguments and returns an integer value. Pay attention to the brackets, they are absolutely necessary.

Here is the code:

#include<stdio.h>

int sum(int a,int b)   //sum is the function returned by call
{
   return a+b;
}

int (*call(void) ) (int ,int);

int main() {
  int (*p)(int,int);   // way to declare a function pointer
  p=call();
  printf("%d\n",(*p)(8,3));
}

int( *call(void) )(int,int) {
    return sum;
}

ReferenceURL : https://stackoverflow.com/questions/11038430/how-to-create-a-typedef-for-function-pointers