How to Create a Typedef for a Function Pointer in C?
Last Updated :
28 Feb, 2024
In C, a function pointer is a variable that stores the address of a function that can later be called through that function pointer. The typedef is a keyword used to create an alias or alternative name for the existing data types. In this article, we will learn how to create a typedef for a function pointer in C.
Typedef for a Function Pointer in C
To create a typedef for a function pointer, we specify the return type of the function, followed by an asterisk (*) and the name of the typedef in parentheses. We also need to specify the number and types of parameters of the function pointer.
Syntax
typedef return_type (*alias_name)(parameter_types and numbers....);
Here, return_type is the return type of the function alias-name is the alias we want to give to the function pointer type and parameter_types are the types of the parameters the function takes.
C Program to Create a Typedef for a Function Pointer in C
C
#include <stdio.h>
typedef int (*Operation)( int , int );
int add( int a, int b) { return a + b; }
int subtracts( int a, int b) { return a - b; }
int main()
{
Operation operationAdd = add;
Operation operationSubtract = subtracts;
printf ( "Addition result: %d\n" , operationAdd(20, 9));
printf ( "Subtraction result: %d\n" ,
operationSubtract(20, 9));
return 0;
}
|
Output
Addition result: 29
Subtraction result: 11
Share your thoughts in the comments
Please Login to comment...