Detail the function pointer in the C language structure

  • 2020-05-10 18:30:12
  • OfStack

A struct is a collection of 1 series data of the same type or different types. As a result, structs in standard C are not allowed to contain member functions, and of course structs in C++ extend this. So, in the structure of C language, we can only use the function pointer to point to the corresponding function by defining the function pointer to achieve the purpose of calling the function.

A function pointer

Function type (* pointer variable name) (parameter list); The first parenthesis must be 1.
"Function type" indicates the return type of the function, and since "()" has a higher priority than "*", parentheses outside the pointer variable name are necessary.
  note that pointer functions are not to be confused with the way function Pointers are represented. The easiest way to tell is if the pointer * before the function name is included in parentheses (). If so, it is a function pointer, and if not, it is a pointer function.
To declare a function pointer, use the following syntax:


Return Type ( * function pointer's variable name ) ( parameters ) 

For example, declare a function pointer named func, receive two integer arguments and return an integer value


int (*func)(int a , int b ) ; 

Type definition can be conveniently applied to function Pointers:


typedef int (*func)(int a , int b ) ; 

Function Pointers in structs
We first define a function pointer named Operation:


typedef int (*Operation)(int a , int b );

Define a simple structure called STR


typedef struct _str {
    int result ; //  To store the results 
    Operation opt; //  A function pointer  

 } STR;

Now let's define two functions: Add and Multi:


//a and b add 
int Add (int a, int b){
  return a + b ;
}
//a and b multiply 
int Multi (int a, int b){
  return a * b ;
}

Now we can write the main function and point the pointer to the correct function:


int main (int argc , char **argv){
   STR str_obj;
   str_obj.opt = Add;  // The function pointer variable points to Add function 
   str_obj. result = str_obj.opt(5,3);
   printf (" the result is %d\n", str_obj.result );
   str_obj.opt= Multi;  // The function pointer variable points to Multi function  
   str_obj. result = str_obj.opt(5,3);
   printf (" the result is %d\n", str_obj.result );
   return 0 ;
}

The results are as follows:


 the result is 8
 the result is 15 

The complete code is as follows:


#include<stdio.h>

typedef int (*Operation)(int a, int b);
typedef struct _str {
  int result ; // to sotre the resut
  Operation opt; // funtion pointer 
 } STR;

//a and b add 
int Add (int a, int b){
  return a + b ;
}

//a and b multiply 
int Multi (int a, int b){
  return a * b ;
}

int main (int argc , char **argv){
   STR str_obj;
   str_obj.opt = Add;  // The function pointer variable points to Add function 
   str_obj. result = str_obj.opt(5,3);
   printf ("the result is %d\n", str_obj.result );
   str_obj.opt= Multi;  // The function pointer variable points to Multi function  
   str_obj. result = str_obj.opt(5,3);
   printf ("the result is %d\n", str_obj.result );
   return 0 ;
}


Related articles: