Typedef Function Pointer in C
Typedef Function Pointer in C
The typedef keyword is used to make a new type alias in C. It enables you to establish a unique name for a function pointer type when used with function pointers, making the code easier to understand and maintain. The C syntax of typedef function pointers is as follows:
Syntax:
typedef return_type (*type_name)(parameter_list);
Now let's examine the syntax:
- return_type: It denotes the function's return type to which the function pointer will point.
- type_name: The type_name is given name to the function pointer type.
- parameter_list: The list of arguments that the function accepts is specified. You can leave the function's arguments field empty or use (void) if it doesn't accept any input.
Pointers to functions
In C, pointers to functions enable you to save a function's memory location in a variable and subsequently call the function using that variable. This gives your code flexibility and allows for dynamic behaviour.
Following are the steps for declaring, allocating, and using pointers to functions in C:
1. Declaration
When you define a pointer to a function, you must include the function's return type and parameter list.
For instance:
return_type (*ptr_name)(parameter_list);
2. Assignment
You need just supply the function's address to the pointer in order to assign a function to it.
For instance, suppose your function is called myFunction:
return_typemyFunction(parameter_list) {
// Function implementation
}
You may provide a function pointer to the address of myFunction:
return_type (*ptr_name)(parameter_list);
ptr_name = myFunction
3. Invocation
Once the function has been assigned to the function pointer, it may be called by using the function call operator () or the dereference operator *.
For instance:
(*ptr_name)(arguments);
// or
ptr_name(arguments);
Both forms (*ptr_name)(arguments) and ptr_name(arguments) are equivalent and will call the function pointed to by the function pointer.
Here is a detailed illustration of how to use pointers to functions:
Example:
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int main() {
int (*ptr)(int, int);
ptr = add;
int result = ptr(15, 30);
printf("Addition: %d\n", result);
ptr = subtract;
result = ptr(15, 30);
printf("Subtraction: %d\n", result);
return 0;
}
Output:
Addition: 45
Subtraction: -15
Explanation:
In the last example, we declared a pointer that points to a function ptr that takes two int parameters and returns an int. We call the add function after assigning its address to the pointer. After that, we call the subtract function by assigning its address to the pointer.
Examples:
1. Callback function
#include <stdio.h>
typedef void (*CallbackFunc)(int);
void processNumbers(int* numbers, int size, CallbackFunc callback) {
for (int i = 0; i< size; i++) {
callback(numbers[i]);
}
}
void printNumber(int num) {
printf("%d ", num);
}
void squareNumber(int num) {
printf("%d ", num * num);
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Numbers: ");
processNumbers(numbers, size, printNumber);
printf("\nSquared numbers: ");
processNumbers(numbers, size, squareNumber);
return 0;
}
Output:
Numbers: 1 2 3 4 5
Squared numbers: 1 4 9 16 25
Explanation:
In this illustration, we create a typedef for a function pointer called CallbackFunc that accepts an integer parameter and returns void. An array of numbers, the array's size, and a callback function pointer are sent to the processNumbers function. It cycles over the integers, calling the callback function for each one. PrintNumber and SquareNumber are two callback functions that we construct in the main function and send as arguments to processNumbers.
2. Function Selector
#include <stdio.h>
typedef int (*MathOperation)(int, int);
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
int calculate(MathOperation operation, int a, int b) {
return operation(a, b);
}
int main() {
int a = 5;
int b = 3;
MathOperation op;
op = add;
printf("Addition: %d\n", calculate(op, a, b));
op = subtract;
printf("Subtraction: %d\n", calculate(op, a, b));
op = multiply;
printf("Multiplication: %d\n", calculate(op, a, b));
return 0;
}
Output:
Addition: 8
Subtraction: 2
Multiplication: 15
Explanation:
In this example, a function pointer that accepts two integer inputs and outputs one integer is given a MathOperation typedef. The calculate function accepts two numbers and a function pointer of type MathOperation. It then calls the function pointer using the supplied values. The add, subtract, and multiplication functions are defined in the main function and assigned to the op function pointer variable. The compute function is then called using various techniques.