Structure and function pointers

Definition of the structure of the pointer variable

The general form of the structure variable is defined as follows:
Form 1: First define the type of structure, and then define the variable
struct structure identifier
{
  variable list members; ...
};
struct structure identifier pointer variable * name; product
variables are initialized: struct structure identifier variable name = initial value 1, the initialization value 2, ..., n-initialization value};

form 2: definition of variables defined type while
struct structure identifier
{
  member variable list; ...
} * pointer variable name;

form 3 : direct define variables directly with unknown structure variables defined only once
struct
{
  member variable list; ...
} * pointer variable name;

wherein "pointer variable name" is a name of the structure pointer variable. 1 is a first form definition of the structure, then the definition of the structure of this type of pointer variables; type 2 and type 3 is the structure definition of this pointer variable in the definition of the structure at the same time.

Function pointer defined
general function pointer can be so defined:
  int (FUNC *) (int, int);
denotes a pointer containing two int parameters and the return value is a function pointer int any form if there is a function:
ADD2 int (int X, Y int)
{
  return X + Y;
}
it can be achieved in actual pointer func:
func = & add2; // pointer assignments, or func = add2; add2 & add2 the same meanings as
printf ( "func (3,4) = % d \ n", func (3,4));

in fact, the code for transplantation consider, typically using typedef defined function pointer type .
typedef int (* FUN) (int, int); // see below

/ * Typedef int (* funcptr) (); This means: int define a return value, the function pointer with no parameters,
that is funcptr pointer is int (*) () type
funcptr table [10]; 
defined an array, the array is funcptr type. That content within this array is a pointer that points to a return value int, takes no arguments * /
FUN & FUNC = ADD2;
FUNC ();

Structure contains function pointers
actually in the structure, it may be a general variable like, comprising a function pointer variable. Here is a simple implementation.

Copy the code
Copy the code
#include <stdio.h> 
struct the DEMO   
{   
    int X, Y;   
    int (FUNC *) (int, int); // function pointer   
};   
  
int ADD1 (int X, Y int)   
{   
    return X * Y;   
}   

int ADD2 (int X, Y int)   
{   
    return X + Y;   
}   

void main ()   
{   
    struct the DEMO Demo;   
    demo.func = ADD2; // structure function pointer assignment   
    //demo.func=&add2; // function pointer structure assignment   
    the printf ( "FUNC (3,4-) D =% \ n-", demo.func (3,4-));   
    demo.func = ADD1;   
    the printf ( "FUNC (3,4-) D =% \ n-", Demo .FUNC (3,4-));   
} 
after performing the terminal display: 
FUNC (3,4-) =. 7 
FUNC (3,4-) = 12 is
Copy the code
Copy the code

Structural body pointer to a function

C language struct is closest to the concept of class, struct in C language, but only members can not have a function, but there may be a pointer to a function, which also facilitated our use of the function. For example, the following:

Copy the code
Copy the 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");  
    // No malloc also works correctly in VC and TC, but in linux gcc error occurs, the error for the segment must be used malloc  
{  
Int main ()  
}  
      
    stu *stu1;  
    stu1 = (stu *) malloc (   sizeof (stu)); 
    when // used must first initialize   
    stu1-> the above mentioned id = 1000;   
    strcpy (stu1-> name, "C ++");   
    stu1-> = Initial Initial;   
    STU1 -> = Process Process;   
    stu1-> = the destroy the destroy;   
    the printf ( "% D \ T% S \ n-", stu1-> ID, stu1-> name);   
    stu1-> Initial ();   
    stu1-> Process (STU1 -> ID, stu1-> name);   
    stu1-> the destroy ();   
    Free (STU1);   
    return 0;   
} 
terminal display:
1000 C ++
Initialization ...
Process ...
1000 C ++
the destroy ..
Copy the code
Copy the code

c language, how to realize the function function in the structure? The structure and made of similar type, so that his internal attributes, there are methods,
such a structure is generally called a protocol type, reference: 
struct { 
  int FUNCID; 
  char * funcname; 
  int (* funcint) (); / function pointer of type int * * / 
  void (* funcvoid) (); / * function pointer type void * / 
}; 
always need to be initialized, troublesome

Copy the code
Copy the code
#include <stdio.h>   
      
typedef struct   
{   
    int A;   
    void (pshow *) (int);   
} the TMP;   
      
void FUNC (the TMP * tmp)   
{   
    IF (tmp-> A> 10) // If a> 10, the callback function.  
    {   
        (Tmp-> pshow) (tmp-> a);   
    }   
}   
      
void Show (int a)   
{   
    the printf ( "D value of a is% \ n-", a);   
}   
      
void main ()   
{ 
    the TMP Test;   
    Test. 11 = a;   
    test.pshow = Show;   
    FUNC (Test &);   
}   
terminal display:
the value of a is 11 / * callback function is the general usage: Party definition structure (including members callback function pointer) B definition of the structure variables and registers Party, a collecting B side of the N registration list structure is formed, traversing the list at a specific time, callbacks. When the function pointer as a parameter to the function, is passed to a function is called, the called function can be an external function call by this pointer, which formed a callback <p> General procedure callback function is not very obvious effect may not be using this form </ p> <p> the main
purpose is to function as a file which is not in the same, such as a dynamic library, the function to call another program only in the form of callback function pointer by an external parameter function address of the incoming tone is achieved
with </ p> <p> Code has been modified function, do not have to change the code base, you can normally achieve ease of maintenance and upgrade program calls </ p> * /
source: HTTPS: //www.cnblogs.com/lvjunjie/p/8961731.html

Guess you like

Origin www.cnblogs.com/llanse-xianchengduo/p/10961936.html