Define function pointer in structure

 

 

Definition of structure pointer variable

The general form of defining a structure variable is as follows:
Form 1: first define the structure type, and then define the variable
struct structure identifier
{
  member variable list; ...
};
struct structure identifier * pointer variable name;
variable initialization: struct structure Identifier variable name = {initialization value 1, initialization value 2, ..., initialization value n }; form 2: define the variable struct structure identifier

while defining the type {   member variable list; ... } * pointer variable name; form 3 : Define variables directly, and define variables directly with an unnamed structure only once struct {   list of member variables; ... }* pointer variable name; where "pointer variable name" is the name of the structure pointer variable. Form 1 is to define the structure first, and then define the structure pointer variable of this type; Form 2 and Form 3 define the structure pointer variable of this type at the same time as the structure is defined. Definition of function pointers A general function pointer can be defined like this: int (*func)(int,int); means a pointer to any function that takes two int parameters and returns an int value. If there is such a function: int add2(int x,int y) {   return x+y; } Then when actually using the pointer func, it can be implemented like this:















  






func=&add2; //Pointer assignment, or func=add2; add2 has the same meaning as &add2
printf("func(3,4)=%d\n",func(3,4));

In fact, for code porting Consider, generally use typedef to define function pointer types .
typedef int (*FUN)(int,int); //Refer to the following

/* typedef int (*funcptr)(); This means: define a function pointer with a return value of int and no parameters, that is,
funcptr is a pointer of type int (*)()
funcptr table[10]; 
Definition An array of type funcptr. That is to say, the content of this array is a pointer, which points to a function with a return value of int and no parameters */
FUN func=&add2;
func();

The structure contains function pointers.
In fact, the structure can also contain function pointer variables like general variables. The following is a simple implementation.

copy code
#include <stdio.h>
struct DEMO  
{  
    int x,y;  
    int (*func)(int,int); //function pointer  
};  
  
int add1 (int x, int y)  
{  
    return x*y;  
}  

int add2(int x,int y)  
{  
    return x+y;  
}  

void main()  
{  
    struct DEMO demo;  
    demo.func=add2; //Structure function pointer assignment  
    //demo.func=&add2; //Structure function pointer assignment  
    printf("func(3,4)=%d\n",demo.func(3,4));  
    demo.func=add1;  
    printf("func(3,4)=%d\n",demo.func(3,4));  
}
After execution, the terminal displays:
func(3,4)=7
func(3,4)=12
copy code

pointer to function in struct

The struct in the C language is the concept closest to the class, but in the struct of the C language, there are only members, not functions, but there can be pointers to functions, which is convenient for us to use functions. For example, as follows:

copy code
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
      
typedef struct student  
{  
    int id;  
    char name[50];   
    void (*initial)();  
    void (*process)(int id, char *name);  
    void (*destroy)();  
}stu;  
      
void initial()  
{  
    printf("initialization...\n");  
}  
      
void process(int id, char *name)  
{  
    printf("process...\n%d\t%s\n",id, name);  
}  
     
void destroy()  
{  
    printf("destroy...\n");  
}  
      
intmain()  
{  
    stu * stu1;  
    //It can run normally without malloc under VC and TC, but there will be an error under linux gcc, it is a segmentation fault, malloc must be used  
    stu1=(stu *)malloc(sizeof(stu));  
    // must be initialized before use  
    stu1->id=1000;  
    strcpy(stu1->name,"C++");  
    stu1->initial=initial;  
    stu1->process=process;  
    stu1->destroy=destroy;  
    printf("%d\t%s\n",stu1->id,stu1->name);  
    stu1->initial();  
    stu1->process(stu1->id, stu1->name);  
    stu1->destroy();  
    free(stu1);  
    return 0;  
} 
The terminal shows:
1000 C++
initialization...
process...
1000 C++
destroy..
copy code

In the c language, how to implement the function of the function in the structure? Make a structure similar to a class, and let it have properties and methods inside.
Such a structure is generally called a protocol class and provides a reference: 
struct { 
  int funcid; 
  char *funcname; 
  int (*funcint)(); / * Function pointer int type*/ 
  void (*funcvoid)(); /* Function pointer void type*/ 
}; 
It needs to be initialized every time, which is troublesome

copy code
#include <stdio.h>  
      
typedef struct  
{  
    int a;  
    void (*pshow)(int);  
}TMP;  
      
void func(TMP *tmp)  
{  
    if(tmp->a >10)//If a>10, execute the callback function.  
    {  
        (tmp->pshow)(tmp->a);  
    }  
}  
      
void show(int a)  
{  
    printf("The value of a is %d\n",a);  
}  
      
void main()  
{
    TMP test;  
    test.a = 11;  
    test.pshow = show;  
    func(&test);  
}   The 
terminal displays:
the value of a is 11 /*The usage of the general callback function is: Party A defines the structure (members include pointers to callback functions) Party B defines structure variables and registers with Party A, Party A collects N registrations of Party B to form a linked list of structures, traverses the linked list at a specific moment, and calls back. When a function pointer is passed as a function parameter to a called function, The called function can call external functions through this pointer, which forms a callback.<p>In general programs, the function of the callback function is not very obvious, so it is not necessary to use this form.</p><p>The main
purpose is to When the function is not in the same file, such as a dynamic library, the only way to call functions in other programs is to use the form of callback, and pass in the address of the external function through the function pointer parameter to realize the
call </p><p>function If the code is modified, and the code of the library does not need to be changed, the call can be implemented normally, which is convenient for program maintenance and upgrade</p>*/
copy code

Definition of structure pointer variable

The general form of defining a structure variable is as follows:
Form 1: first define the structure type, and then define the variable
struct structure identifier
{
  member variable list; ...
};
struct structure identifier * pointer variable name;
variable initialization: struct structure Identifier variable name = {initialization value 1, initialization value 2, ..., initialization value n }; form 2: define the variable struct structure identifier

while defining the type {   member variable list; ... } * pointer variable name; form 3 : Define variables directly, and define variables directly with an unnamed structure only once struct {   list of member variables; ... }* pointer variable name; where "pointer variable name" is the name of the structure pointer variable. Form 1 is to define the structure first, and then define the structure pointer variable of this type; Form 2 and Form 3 define the structure pointer variable of this type at the same time as the structure is defined. Definition of function pointers A general function pointer can be defined like this: int (*func)(int,int); means a pointer to any function that takes two int parameters and returns an int value. If there is such a function: int add2(int x,int y) {   return x+y; } Then when actually using the pointer func, it can be implemented like this:















  






func=&add2; //Pointer assignment, or func=add2; add2 has the same meaning as &add2
printf("func(3,4)=%d\n",func(3,4));

In fact, for code porting Consider, generally use typedef to define function pointer types .
typedef int (*FUN)(int,int); //Refer to the following

/* typedef int (*funcptr)(); This means: define a function pointer with a return value of int and no parameters, that is,
funcptr is a pointer of type int (*)()
funcptr table[10]; 
Definition An array of type funcptr. That is to say, the content of this array is a pointer, which points to a function with a return value of int and no parameters */
FUN func=&add2;
func();

The structure contains function pointers.
In fact, the structure can also contain function pointer variables like general variables. The following is a simple implementation.

copy code
#include <stdio.h>
struct DEMO  
{  
    int x,y;  
    int (*func)(int,int); //function pointer  
};  
  
int add1 (int x, int y)  
{  
    return x*y;  
}  

int add2(int x,int y)  
{  
    return x+y;  
}  

void main()  
{  
    struct DEMO demo;  
    demo.func=add2; //Structure function pointer assignment  
    //demo.func=&add2; //Structure function pointer assignment  
    printf("func(3,4)=%d\n",demo.func(3,4));  
    demo.func=add1;  
    printf("func(3,4)=%d\n",demo.func(3,4));  
}
After execution, the terminal displays:
func(3,4)=7
func(3,4)=12
copy code

pointer to function in struct

The struct in the C language is the concept closest to the class, but in the struct of the C language, there are only members, not functions, but there can be pointers to functions, which is convenient for us to use functions. For example, as follows:

copy code
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
      
typedef struct student  
{  
    int id;  
    char name[50];   
    void (*initial)();  
    void (*process)(int id, char *name);  
    void (*destroy)();  
}stu;  
      
void initial()  
{  
    printf("initialization...\n");  
}  
      
void process(int id, char *name)  
{  
    printf("process...\n%d\t%s\n",id, name);  
}  
     
void destroy()  
{  
    printf("destroy...\n");  
}  
      
intmain()  
{  
    stu * stu1;  
    //It can run normally without malloc under VC and TC, but there will be an error under linux gcc, it is a segmentation fault, malloc must be used  
    stu1=(stu *)malloc(sizeof(stu));  
    // must be initialized before use  
    stu1->id=1000;  
    strcpy(stu1->name,"C++");  
    stu1->initial=initial;  
    stu1->process=process;  
    stu1->destroy=destroy;  
    printf("%d\t%s\n",stu1->id,stu1->name);  
    stu1->initial();  
    stu1->process(stu1->id, stu1->name);  
    stu1->destroy();  
    free(stu1);  
    return 0;  
} 
The terminal shows:
1000 C++
initialization...
process...
1000 C++
destroy..
copy code

In the c language, how to implement the function of the function in the structure? Make a structure similar to a class, and let it have properties and methods inside.
Such a structure is generally called a protocol class and provides a reference: 
struct { 
  int funcid; 
  char *funcname; 
  int (*funcint)(); / * Function pointer int type*/ 
  void (*funcvoid)(); /* Function pointer void type*/ 
}; 
It needs to be initialized every time, which is troublesome

copy code
#include <stdio.h>  
      
typedef struct  
{  
    int a;  
    void (*pshow)(int);  
}TMP;  
      
void func(TMP *tmp)  
{  
    if(tmp->a >10)//If a>10, execute the callback function.  
    {  
        (tmp->pshow)(tmp->a);  
    }  
}  
      
void show(int a)  
{  
    printf("The value of a is %d\n",a);  
}  
      
void main()  
{
    TMP test;  
    test.a = 11;  
    test.pshow = show;  
    func(&test);  
}   The 
terminal displays:
the value of a is 11 /*The usage of the general callback function is: Party A defines the structure (members include pointers to callback functions) Party B defines structure variables and registers with Party A, Party A collects N registrations of Party B to form a linked list of structures, traverses the linked list at a specific moment, and calls back. When a function pointer is passed as a function parameter to a called function, The called function can call external functions through this pointer, which forms a callback.<p>In general programs, the function of the callback function is not very obvious, so it is not necessary to use this form.</p><p>The main
purpose is to When the function is not in the same file, such as a dynamic library, the only way to call functions in other programs is to use the form of callback, and pass in the address of the external function through the function pointer parameter to realize the
call </p><p>function If the code is modified, and the code of the library does not need to be changed, the call can be implemented normally, which is convenient for program maintenance and upgrade</p>*/
copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324937190&siteId=291194637