Function pointer array

Posted by Lauram340 on Wed, 02 Mar 2022 03:18:52 +0100

Function pointer array

1. Pointer function

Pointer function refers to the function with pointer, that is, it is essentially a function.
All functions have a return type (if they do not return a value, they are valueless), but the return type of pointer function is a pointer of a certain type.
Its definition format is as follows:

Return type identifier *Return name(Formal parameter table)
{ 
	Function body
} 

The return type can be any basic type or composite type. Functions that return pointers are widely used. In fact, every function, even if it does not return a pointer of some type, itself has an entry address, which is equivalent to a pointer. 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 the function itself, and the whole function is equivalent to a "variable".

2. Function pointer

"Function pointer" is a pointer variable pointing to a function, so "function pointer" itself should first be a pointer variable, but the pointer variable points to a function. This is just like using pointer variables to point to integer variables, character types and arrays. Here, it points to functions.
When compiling C, each function has an entry address, which is the address pointed to by the function pointer. After having the pointer variable pointing to the function, you can call the function with the pointer variable, just as you can refer to other types of variables with the pointer variable. These concepts are consistent. Function pointers serve two purposes: calling functions and making arguments to functions.

The description method of function pointer is:

Function type (*Pointer variable name)(parameter list );

"Function type" indicates the return type of the function. Since "()" takes precedence over "*", the parentheses outside the pointer variable name are essential. The following "formal parameter list" represents the parameter list of the function pointed to by the pointer variable.

For example:

int (*f)(int x);

double(*ptr)(double x);

When defining function pointers, please note:

  • The number and type of parameters of the function pointer and the function it points to should be consistent;

  • The type of function pointer and the return value type of function must also be consistent.

Assignment of function pointer

The function name, like the array name, represents the first address of the function code. Therefore, when assigning a value, just point the function pointer directly to the function name.

int func(int x);    /* Declare a function */
int (*f)(int x);    /* Declare a function pointer */
f = func;           /* Assign the first address of func function to pointer f */

When assigning a value, the function func does not contain parentheses or parameters. Since func represents the first address of the function, after assignment, the pointer f points to the first address of the code of the function func(x).

Similar to other pointer variables, if the pointer variable pi is a pointer to an integer variable i, then * p is equal to the variable i it refers to; If pf is a pointer to a floating-point variable F, * pf is equivalent to the variable f it refers to. Similarly, * f is the pointer to the function func(x), then * f represents the function func it points to. So f = func is executed; After that, (* f) and func represent the same function.

Since the function pointer points to a function in the storage area, the corresponding function can be called through the function pointer.
How to call a function with a function pointer, it should perform the following three steps:

first,To specify a function pointer variable.

For example: int (*f)(int x);

secondly,To assign a value to a function pointer variable.

For example: f = func;
(func(x)There must be a definition first)

last,To use(*Pointer variable)(Parameter table);
Call the function.

For example:(*f)(x);
(x You must assign a value first) 

Example 1

int max(int x, int y)
{
    return(x > y ? x : y);
} 
 
int  main(void)
{
    int a, b, c;
    int (*ptr)(int, int);
    scanf("%d,%d", &a, &b);
    ptr = max;
    c = (*ptr)(a, b);
    printf("a=%d,b=%d,max=%d", a, b, c);
    return 0;
}

Example 2

void FileFunc()
{
    printf("FileFunc\n");
} 
 
void EditFunc()
{
    printf("EditFunc\n");
} 
 
int main(void)
{
    void (*funcp)();
    funcp = FileFunc;
    (*funcp)();
    funcp = EditFunc;
    (*funcp)();
}

3. Function pointer array

A function pointer array is an array whose elements are function pointers. In other words, the data structure is an array, and its element is a pointer to the function entry address.
According to the analysis: the first description is an array: array name []
Secondly, the data type pointer of its element should be described: * array name []
Again, make it clear that each array element is a pointer to the function entry address: the function return value type (* array name [])
Please note that why should "* array name []" be expanded in parentheses here? Because parentheses and array specifiers have the same priority, if you do not use parentheses to expand the pointer array description expression, according to the combination direction of parentheses and square brackets, what does * array name [] () describe? Is an array of functions whose element return value type is pointer. Is there such a function number? I don't know Therefore, it must be enclosed to ensure that each element of the array is a pointer.

#include "stdio.h"
int add1(int a1, int b1);
int add2(int a2, int b2); 
 
int main(int argc, char *argv[])
{
    int numa1 = 1, numb1 = 2;
    int numa2 = 2, numb2 = 3;
    int (*op[2])(int a, int b);
    op[0] = add1;
    op[1] = add2;
    printf("%d %d\n", op[0](numa1, numb1), op[1](numa2, numb2));
    getch();
} 
 
int add1(int a1, int b1)
{
    return a1 + b1;
} 
 
int add2(int a2, int b2)
{
    return a2 + b2;
}

4. Comprehensive

Then give the commonly used C Variable definition method:
a) An integer(An integer)
b) A pointer to an integer(A pointer to an integer)
c) A pointer to a pointer,The pointer it points to is an integer number(A pointer to a pointer to an integer)
d) An array of 10 integers(An array of 10 integers)
e) An array of 10 pointers,The pointer points to an integer(An array of 10 pointers to integers)
f) A pointer to a group of 10 integers(A pointer to an array of 10 integers)
g) A pointer to a function,This function takes an integer parameter and returns an integer number(A pointer to a function that takes an integer as an argument and returns an integer)
h) An array of 10 pointers,The pointer points to a function,This function takes an integer parameter and returns an integer number( An array of ten pointers to functions that take an integer argument and return an integer)

The answer is:
a) int a;               // An integer
b) int *a;              // A pointer to an integer
c) int **a;             // A pointer to a pointer to an integer
d) int a[10];           // An array of 10 integers
e) int *a[10];          // An array of 10 pointers to integers
f) int (*a)[10];        // A pointer to an array of 10 integers
g) int (*a)(int);       // A pointer to a function a that takes an integer argument and returns an integer
h) int (*a[10])(int);   // An array of 10 pointers to functions that take an integer argument and return an integer