Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - C language master Jin
C language master Jin
& gt 1:typedef structure {

& gt2:char title[ 100];

& gt3:int(* func)();

& gt4:} tip callbacks;

To put it bluntly, it is a function pointer;

Simply put, such as int add(int, int); int min(int,int); For functions with the same parameters and return values, if you want to handle them in a general way, define a function pointer:

int (*function)(int,int);

In this way, the function pointer can point to any truly defined function:

Function = & Or function =&; min

Function Pointer A function pointer is a pointer variable that points to a function.

So the "function pointer" itself should be a pointer variable first, but the pointer variable points to a function. This is like using pointer variables to point to integer variables, character types and arrays, and here is pointing to functions. As mentioned above, when C compiles, each function has an entry address, which is the address pointed by the function pointer. By pointing a pointer variable to a function, you can call a function with this pointer variable, just as you can refer to other types of variables with a pointer variable. These concepts are consistent. Function pointers are used for two purposes: calling functions and making parameters of functions. The explanation method of function pointer is:

Data type identifier (pointer variable name) (parameter list);

Note 1: "Function Type" indicates the return type of the function. Because "()" has higher priority than "*", parentheses outside the pointer variable name are necessary, and the following "parameter list" indicates the parameter list of the function pointed by the pointer variable. example

int func(int x); /* Declare function */

int(* f)(int x); /* Declare a function pointer */

F = func/* Assign the first address of func function to pointer f */

Function func has no parentheses and no parameters when assigning values. Since func represents the first address of the function, the pointer f points to the first address of the code of func(x) after assignment.

Note 2: The formal parameters in function brackets are optional, depending on the situation.

The following program illustrates the method of function pointer calling function:

Example 1,

# include & ltstdio.h & gt

int max(int x,int y){ return(x & gt; y? x:y); }

void main()

{

int (*ptr)(int,int);

int a,b,c;

ptr = max

Scanf("%d, %d ",& i, & amp);

c=(*ptr)(a,b);

printf("a=%d,b=%d,max=%d ",a,b,c);

}

Ptr is a pointer variable of a function, so you can assign the function max () to ptr as the value of ptr, that is, assign the entry address of max () to ptr, and then you can call this function with ptr. In fact, both ptr and max point to the same entry address, but the difference is that ptr is a pointer variable, unlike a function name, it can point to any function, depending on how you do it. It points to any function in the program whose address is assigned to it. Then use pointer variables to call, so that you can point to different functions in turn, but note that pointer variables pointing to functions have no++and-operations, so be careful when using them.

However, in some compilers, this cannot be passed. The supplement to this example is as follows.

It should be like this:

1. Define the function pointer type:

typedef int (*fun_ptr)(int,int);

2. Declare variables and assign values:

fun _ ptr max _ func = max

In other words, the function assigned to the function pointer should be consistent with the function prototype pointed by the function pointer.

Example 2,

# include & ltstdio.h & gt

void FileFunc()

{

printf(" file func \ n ");

}

void EditFunc()

{

printf(" edit func \ n ");

}

void main()

{

void(* funcp)();

funcp = FileFunc

(* funcp)();

funcp = EditFunc

(* funcp)();

}

The difference between pointer function and function pointer

1, both of which are abbreviations. Pointer function refers to a function with a pointer, that is, a function in essence. We know that all functions have a return type (no return value is no value), but the return type of pointer function is some kind of pointer.

Its definition format is as follows:

Return Type Identifier * Return Name (parameter table)

{Function body}

The return type can be any basic type and composite type. Functions that return pointers are widely used. In fact, each function has its own entry address, which is equivalent to a pointer, even if it does not return a pointer of a certain type. For example, if a function returns an integer value, it is actually equivalent to returning the value of a pointer variable, but the variable at this time is only the function itself, and the whole function is equivalent to a "variable". For example, the following is an example of a function that returns a pointer:

# Including

Floating * Find ();

Master ()

{

Static floating-point score [] [4] = {{60,70,80,90}, {56,89,34,45}, {34,23,56,45}};

float * p;

int i,m;

Printf ("Enter the number to find:");

scanf("%d ",& ampm);

Printf ("The score of %d is: \n", m);

P=find (score, m);

for(I = 0; I<4; i++)

printf(" % 5.2f \ t " ,*(p+I));

}

Float * find (float (* pioneer) [4], int n)/* Define pointer function */

{

float * pt

pt = *(pienter+n);

Return (pt);

}

Student numbers are counted from 0, the function find () is defined as a pointer function, and the parameter pointer is a pointer variable that points to a one-dimensional array containing four elements. The pointer+1 points to the first line score. * (pointer+1) points to the 0th element of the first line. Pt is a pointer variable, pointing to a floating-point variable. Call the find () function in the main () function and pass the first address of the fractional array to the pointer.

2. "Function pointer" is a pointer variable that points to a function, so "Function pointer" itself should be a pointer variable first, but the pointer variable points to a function. This is like using pointer variables to point to integer variables, character types and arrays, and here is pointing to functions. As mentioned above, when C compiles, each function has an entry address, which is the address pointed by the function pointer. By pointing a pointer variable to a function, you can call a function with this pointer variable, just as you can refer to other types of variables with a pointer variable. These concepts are consistent. Function pointers are used for two purposes: calling functions and making parameters of functions.

The explanation method of function pointer is:

Data type identifier (* pointer variable name) (parameter);

Note 1: The parameters in function brackets are optional, depending on the situation. The following program illustrates the method of function pointer calling function:

# Including

int max(int x,int y){ return(x & gt; y? x:y); }

void main()

{

int(* ptr)();

int a,b,c;

ptr = max

Scanf("%d, %d ",& i, & amp);

c=(*ptr)(a,b);

printf("a=%d,b=%d,max=%d ",a,b,c);

}

Ptr is a pointer variable of a function, so you can assign the function max () to ptr as the value of ptr, that is, assign the entry address of max () to ptr, and then you can call this function with ptr. In fact, both ptr and max point to the same entry address, but the difference is that ptr is a pointer variable, unlike a function name, it can point to any function, depending on how you do it. It points to any function in the program whose address is assigned to it. Then use pointer variables to call, so that you can point to different functions in turn, but note that pointer variables pointing to functions have no++and-operations, so be careful when using them.

On the definition of function pointer array

There are two methods to define function pointer array: one is standard method; One is cheating.

First, the standard method:

{

Parsing: Function pointer array is an array whose elements are function pointers. In other words, this data structure is an array, and its elements are pointers to the address of the function entry.

According to the analysis: firstly, it is an array: array name []

Secondly, you need to explain the data type pointer of its elements: * array name [].

Third, make it clear that each array element is a pointer to the function entry address: function return value type (* array name []) (). Please note why "* array name []" is expanded in brackets here. Because parentheses and array descriptors have the same priority, what does * array name [] () mean according to the combination direction of parentheses and square brackets if you don't use parentheses to extend the pointer array to describe the expression? Is an array of functions whose element return value type is pointer. Is there such a function ancestor? I don't know, so I have to enclose it to make sure that every element of the array is a pointer.

}

Second, the deception method:

Although this function is not a variable, it still has its physical address in memory, which can be assigned to the pointer variable. The way to get a function is to use the function name without parentheses and parameters.

The function name is equivalent to the const pointer pointing to its function entry. So since the function name is a pointer constant, you can do some corresponding processing, such as forced type conversion.

Then we can put this address in an array of integer pointers and call it as a function pointer.

Complete example:

# contains "stdio.h"

int add 1(int a 1,int b 1);

int add2(int a2,int B2);

void main()

{

int numa 1= 1,numb 1 = 2;

int numa2=2,numb 2 = 3;

int (*op[2])(int a,int b);

op[0]= add 1;

op[ 1]= add 2;

printf("%d %d\n ",op[0](numa 1,numb 1),op[ 1](numa2,numb 2);

}

int add 1(int a 1,int b 1)

{

Returns a1+b1;

}

int add2(int a2,int b2)

{

Return a2+b2;

}

Then the common definition of C variable is given:

A) integers.

B) a pointer to an integer.

C) pointer to pointer, pointer to integer (pointer to integer)

D) 10 array of integers.

E) An array with 10 pointers, with the pointers pointing to an integer (10 pointers pointing to an array of integers).

F) Pointer to 10 integer array.

G) Pointer to a function with integer parameters and returning an integer (pointer to a function with an integer as an independent variable and returning an integer).

H) An array with 10 pointers to a function that takes an integer parameter and returns an integer (an array of ten pointers points to a function that takes an integer parameter and returns an integer).

The answer is:

a)int a; //Integer

b)int * a; //Pointer to an integer

c)int * * a; //Pointer to integer pointer

d)int a[ 10]; // 10 array of integers

e)int * a[ 10]; //An array of 10 pointers to integers.

f)int(* a)[ 10]; //Pointer to 10 integer array

g)int(* a)(int); //Pointer to function A that takes integer parameters and returns an integer.

h)int(* a[ 10])(int); // 10 array of pointers to functions that take integer parameters and return integers.