/* ##################################################################### # IN THIS SECTION, I WOULD COVER 3 CONCEPTS PERTAINING TO FUNCTION # POINTERS. # 1. How to use Function Pointers. # 2. Typedef for function pointers [i.e. alias names for function # pointer]. # 3. 'Compound literals' for a structure containing function # pointers. ##################################################################### */
/******************************************************
* A function pointer is like any other pointer, but it
* points to the address of a function instead of the
* address of data (on heap or stack). Like any pointer,
* it needs to be typed correctly. Functions are defined
* by their return value and the types of parameters they
* accept. So in order to fully describe a function, you
* must include its return value and the type of each
* parameter is accepts. When you typedef such a
* definition, you give it a 'friendly name' which makes
* it easier to create and reference pointers using that
* definition.
* So for example assume you have a function:
*
* float doMultiplication (float num1, float num2 ) {
* return num1 * num2;
* }
*******************************************************
* then the following is the 'typedef' for a function
* pointer.
******************************************************
* typedef float(*pt2Func)(float, float);
* pt2Func new_alias_name_1;
* pt2Func new_alias_name_2;
* pt2Func new_alias_name_3;
*
* ----> the above is equivalent to <----
* float (*new_alias_name_1)(float, float);
* float (*new_alias_name_2)(float, float);
* float (*new_alias_name_3)(float, float);
*
* Now, we can see how using 'typedef' for 'function
* pointers' is a better and a cleaner approach.
******************************************************/
#include <stdio.h>
int add(int a, int b);
int mul(int a, int b);
/********************************************************
* Define 2 function 'add' and 'multiply' with same
* Function prototype.
********************************************************/
/*
* Add two numbers a and b
*/
int add(int a , int b) {
return (a + b);
}
/*
* Multiply two numbers a and b
*/
int mul(int a, int b) {
return (a * b);
}
/*******************************************************
* typedef of Function pointers of functions add and
* multiply
*******************************************************/
typedef int (*operation)(int a , int b);
/* Define a structure with function pointers as members */
typedef struct do_calculation {
operation add_integer; /* function pointer for 'add' function */
operation multiply_integer; /* function pointer for 'mul' function */
} math;
/***********************************************************
* Designated Initialization With 'Compound Literals' in C
*
* Refer this weblink for more information on compound literals.
* http://nickdesaulniers.github.io/blog/2013/07/25/designated-initialization-with-pointers-in-c/
***********************************************************/
math calc =
{
.add_integer = add,
.multiply_integer = mul,
};
int main(int argc, char **argv)
{
int result_add ; /* to store the result */
int result_mul ; /* to store the result */
result_add = calc.add_integer(3, 4);
result_mul = calc.multiply_integer(3, 4);
printf ("the result is %d\n", result_add);
printf ("the result is %d\n", result_mul);
return 0;
}
Output:
./a.out
the result is 7
the result is 12