pointers to arrays and arrays of pointers, pointers to functions and pointers to functions

Pointer array and array pointer are two different concepts, and their difference lies in the difference in definition and usage.

An array of pointers refers to an array in which several pointers are stored. The way to define a pointer array is to add an * before the type name, for example `int *arr[10]`, which means that an array containing 10 elements is defined, and each element is a pointer to integer data. When using an array of pointers, you can access the pointers in the array through subscripts. For example, `arr[0]` means the first element in the array, which is a pointer to integer data.

An array pointer is a pointer that points to an array. The way to define an array pointer is to add a * sign after the type name, such as `int (*p)[10]`, which means that a pointer to an integer array containing 10 elements is defined. When using an array pointer, you need to point the pointer to an array first, such as `int a[10]; p = &a`, which means pointing the pointer `p` to the first address of the array `a`. The elements in the array can then be accessed through pointers, for example `(*p)[0]` means the first element in the array.

Pointer functions and function pointers are two different concepts, and their difference lies in the relationship between functions and pointers.

A pointer function is a function that returns a pointer. The way to define a pointer function is to add a * before the function name, such as `int *func(int n)`, which means that a function is defined, it accepts an integer parameter `n`, and returns a pointer to integer data pointer. When using a pointer function, the pointer can be obtained through the return value of the function, for example, `int *p = func(10)` means calling the function `func` and assigning the return value to the pointer `p`.

A function pointer is a pointer that points to a function. The way to define a function pointer is to add a * after the type name, such as `int (*p)(int)`, which means that a pointer to a function whose parameters are integer data and return value is integer data is defined . When using a function pointer, you need to point the pointer to a function first, such as `int func(int n) { return n * 2; } p = &func`, which means pointing the pointer `p` to the address of the function `func`. Then the function can be called through the pointer, for example `int res = (*p)(10)` means to call the function `func` through the pointer and assign the return value to the variable `res`.

Function pointers can be used to implement callback functions. Callback functions refer to the situation where a function is passed as a parameter to another function and is called in another function. Callback functions are often used in scenarios such as event processing and asynchronous programming.

Here's an example of how to implement a callback function with a function pointer:
#include <iostream>

using namespace std;

void print_hello() {
    cout << "Hello, world!" << endl;
}

void call_back_function(void (*func)()) {
    func();
}

int main() {     call_back_function(print_hello); // Pass the print_hello function as a parameter to the call_back_function function and be called there     return 0; }


In this example, we define two functions `print_hello` and `call_back_function`. The `print_hello` function is used to output the string "Hello, world!", and the `call_back_function` function receives a function pointer as a parameter and calls the function in the function body.

In the `main` function, we pass the `print_hello` function as a parameter to the `call_back_function` function, and it is called there. In this way, when we run the program, the string "Hello, world!" will be output.

Guess you like

Origin blog.csdn.net/weixin_43271137/article/details/130039412