(9) Pointer functions and function pointers

Pointer declaration:

A pointer is actually a variable. The declaration method of a pointer is similar to a general variable declaration, as follows:

int *p;             // 声明一个 int类型的指针,该指针指向一个int类型的对象
char *p;            // 声明一个 char类型的指针,该指针指向一个 char类型的对象
int *arr[10];       // 声明一个指针数组, 该数组有10个元素,其中每个元素都是一个指向int 类型对象的指针
int*arr)[10];    // 声明一个数组指针,该指针指向一个 int 类型的以为数组
int **p;            // 声明一个二级指针p,该指针指向一个 int 类型的指针

Declaring a pointer variable does not automatically allocate any memory. Before indirect access to the pointer, the pointer must be initialized: either make it point to the existing memory, or dynamically allocate memory to it, otherwise we don't know where the pointer points. This issue requires special attention.

What is a function pointer?

Function pointer definition : A function pointer is a pointer variable that points to a function. Therefore, the "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 is pointing to functions.

The general expression is :Type specifier (*function name) (parameter)

int (*fun)(int x);        // 函数指针的定义
int (*fun)(int x,int y);  // 函数指针的定义

Assignment of
function pointers Function pointers need to be initialized before being used after they are defined as other pointers.
Function pointers have two purposes: calling functions and making function parameters

int (*fun)(int x,int y);  // 函数指针的定义
fun=&Function;            // 函数指针的赋值方式1
fun=Function;             // 函数指针的赋值方式2
x=(*fun)();               // 函数指针的调用方式1
x=fun();                  // 函数指针的调用方式2,错误的方式

It is not necessary to take the address operator & when assigning a function, because a function identifier indicates its address, and the function does not require parentheses when assigning;

If it is a function call, it must also include a parameter list enclosed in parentheses.

The use of function pointers When
we use pointers, we need to use the key * to get the value in its memory, and the same is true for function pointers. Take out the function stored at this address through (*pf), and then call it.


char* fun(char * p1,char* p2)
{
    
    
int i=0;
i=strcmp(p1,p2);
if(i==0)
{
    
    
return p1;
}
else
{
    
    
return p2;
}
}

int main()
{
    
    
char *(*pf)(char *,char *);
pf=&fun;
(*pf)("a","b");
return 0;
}

What is a pointer function?

**Definition of pointer function: **The foothold of a pointer function is a function, and the return value of this function is a pointer, which is similar to the ordinary function int function(int, int), except that the returned data type is different.

_type_ *function(int, int);   // 返回的是指针地址
int function(int, int);       // 返回的是int类型的数据
int *fun(int x);              // 指针函数的定义
int * fun(int x, int y);      // 指针函数的定义
int* fun(int x,int y);        // 指针函数的定义

The above three ways of writing are correct, but * is easier to understand near the return value.

Pointer to the function call
when calling the function pointer, a need to receive the same type of return value of a pointer function thereof.

typedef struct _Data{
    
    
int a;
int b;
} Data;

// 指针函数
Data* f(int a,int b)
{
    
    
Data *data=new Data;
data->a=a;
data->b=b;
return data;
}

int main()
{
    
    
Data *myData=f(4,5);
cout<<"f(4,5) = " << myData->a << myData->b;
return 0;
}

However, you can also define its return value as void* type, and cast the return value to the type you want when you call it.

The output result is the same, but it is not recommended to use it, because forced conversion may bring risks. The return type can be any basic type and composite type. Functions that return pointers are very versatile.

In fact, every function, even if it does not return a pointer of a certain type, has an entry address in itself, which is equivalent to a pointer.

For example, a function returning an integer value is actually equivalent to returning the value of a pointer variable, but the variable at this time is just the function itself, and the entire function is equivalent to a "variable".


The difference between a function pointer and a pointer function

1. Different definitions

  • A pointer function is essentially a function, and its return value is a pointer.

  • The function pointer is essentially a pointer, which points to a function.

2. Different writing

  • Pointer function: int* fun(int x,int y);

  • Function pointer: int (*fun)(int x,int y);

It can be simply and roughly understood that the * of the pointer function belongs to the data type, and the asterisk of the function pointer belongs to the function name.

To make it simpler, you can distinguish between the two in this way: the function name with parentheses is the function pointer, otherwise it is the pointer function.

3. The usage is different. The
above function pointers and pointer functions are used, but the usage of function pointers will be more, and it is relatively more difficult, such as function pointers and callback functions. If it is a C++ non-static member function pointer, its usage There are also some differences. Interested students can follow follow-up tweets or read related books on their own.

Guess you like

Origin blog.csdn.net/qq_40329851/article/details/114951175