[Annoying] - function pointer function pointer array pointer to function pointer array pointer

We have been learning the C language for a long time, and I believe that everyone is not unfamiliar with this problem. Today, we will once again recognize some basic differences between these functions that cause us headaches.

function pointer

Function pointer: A function pointer is a pointer to an executable code segment or a block of information that calls an executable code segment, not a pointer to some kind of data. Function pointers store and manage functions as ordinary data. A function pointer has a fixed form that contains a definite return value type and several function parameters. Declaring a function pointer looks similar to declaring a function, except that the function name is preceded by a pointer to indicate the pointer , and the function name and are enclosed in parentheses. For example, in the following piece of code, match is declared as a function pointer, which accepts two parameters of void pointer type and returns an integer type.
int (*match)(void* key1,void* key2)
The declaration of the function above means that we specify a pointer that receives two void pointers and returns an integer named match function. For example: Suppose there is a pointer to the match_int function, and its two void pointer parameters point to integers and return 1. Considering that the previous function declaration is match, we can assign values ​​like this.
match=match_int
To execute a function referenced by a function pointer, we just need to call the function pointer where we would normally call a normal function, eg: to call the function pointer match mentioned before, we execute the following statement, assuming x, y and retval has been declared as an integer.
retval=match(&x,&y)
The most important use of function pointers is to encapsulate functions into data structures.
In my understanding, the address of the function pointer is the address of the first instruction, which can be the function name of the function, or the address of the function. The function name represents the address of the function (the function name has an engraved readable Attributes).
Let's look at the code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

void test(){
    printf("hehe\n");
}
int main()
{
    printf("%p\n", test);
    printf("%p\n", &test);
    getchar();
    return 0;
}

The result of the output is:
write picture description here
we can see that the output is two addresses, which are the addresses of the test function.
So how do we save the address of our function to save it?
See the code below:

void test()
{
printf(“hehe\n”);
}
//下面的pfun1和pfun2那哪个有能力存放test函数的地址?
void (*pfun1)();
void *pfun2();//()的优先级高于*

First of all, the address that can be stored is to require pfun1 or pfun2 to be a pointer. Which is the pointer?
the answer is:

pfun1 can be stored, pfun1 is first combined with *, indicating that pfun1 is a pointer, the pointer points to a function, the pointed function has no parameters, and the return value type is void*;

array of function pointers

An array is a storage space that stores data of the same type, so we have already learned about arrays of pointers. For example,
int *arr[10]//数组中的每个元素是int*
if you want to store the address of a function in an array, this array is called an array of function pointers. How is the array of function pointers defined:

`int (*parr1[10]) ();
int    *parr2[10]();
int    (*)() parr3;

The answer is parr1
parr1 is first combined with [ ], indicating that parr1 is an array, what is the content of the array? is a function pointer of type int (*)().

pointer to array of function pointers

We but literally this is a pointer, it points to an array of function pointers defining a pointer. p is first combined with *, it is a variable that points to an array of function pointers, and p stores the address of the entire array. `(*p) 1 ; (*p) means to access the element Add whose index is 1 in the array, and then call the Add function with the parameters 1 and 2.
int (*arr[5])(int ,int)={NULL,Add,Sub,Mul,Div};
int (*(*p)[5])(int ,int)=&arr;

Guess you like

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