Function pointers, pointer functions, and function arrays in C language

function pointer

is a pointer to a function that allows you to dynamically select which function to call while your program is running. Function pointers can be passed, stored and used like normal variables, which makes them very useful in many programming scenarios, such as callback functions, function tables, plug-in architecture, etc.

Here is a simple example to illustrate the concept of function pointers:

#include <stdio.h>

// 声明两个函数,它们具有相同的参数和返回类型
int add(int a, int b) {
    
    
    return a + b;
}

int subtract(int a, int b) {
    
    
    return a - b;
}

int main() {
    
    
    // 声明一个函数指针,它可以指向具有相同签名(参数和返回类型)的函数
    int (*operation)(int, int);

    // 将函数指针指向 add 函数
    operation = add;

    // 使用函数指针调用 add 函数
    int result = operation(5, 3);
    printf("Result of add: %d\n", result);

    // 将函数指针指向 subtract 函数
    operation = subtract;

    // 使用函数指针调用 subtract 函数
    result = operation(5, 3);
    printf("Result of subtract: %d\n", result);

    return 0;
}

//定义函数后,首先声明一个函数指针 将这个函数指针指向这个函数,那么这个指针就有了这个函数的性质,就是函数指针

Function array:

A function array is an array whose elements are function pointers, which allows you to store multiple different functions in an array, which can then be selected and called as needed. This is useful for writing generic code, callback functions, and dynamic function selection. The following is a simple C/C++ example that demonstrates the basic use of function arrays:

#include <stdio.h>

// 定义一些函数,它们将作为函数指针数组的元素
int add(int a, int b) {
    
    
    return a + b;
}

int subtract(int a, int b) {
    
    
    return a - b;
}

int multiply(int a, int b) {
    
    
    return a * b;
}

int divide(int a, int b) {
    
    
    if (b != 0) {
    
    
        return a / b;
    } else {
    
    
        printf("Error: Division by zero\n");
        return 0;
    }
}

int main() {
    
    
    // 声明一个函数指针数组,元素类型是指向函数的指针
    int (*mathFunctions[])(int, int) = {
    
    
        add,
        subtract,
        multiply,
        divide
    };

    int a = 10, b = 5;
    for (int i = 0; i < 4; i++) {
    
    
        int result = mathFunctions[i](a, b); // 使用函数指针数组调用不同的函数
        printf("Result of operation %d: %d\n", i + 1, result);
    }

    return 0;
}

在上述示例中,我们首先定义了四个不同的数学函数 add、subtract、multiply 和 divide,它们都接受两个整数参数并返回一个整数结果。然后,我们声明了一个函数指针数组 mathFunctions,该数组的元素是指向这些函数的指针。

在 main 函数中,我们使用循环遍历 mathFunctions 数组,并通过函数指针调用不同的数学操作。这使得我们可以在运行时选择要执行的函数,而不需要在代码中硬编码不同的操作。函数数组是实现通用和可扩展代码的有用工具。

pointer function

A pointer function is a function that returns a pointer. It is a function whose return type is a pointer type.
The declaration of a pointer function is similar to an ordinary function declaration, but the return type is a pointer.
Pointer functions are usually used to return dynamically allocated memory or a pointer to an object.

#include <stdio.h>

// 定义一个指针函数,该函数返回一个整数指针
int* pointerToFunction() {
    
    
    static int num = 42; // 静态变量,保持其值在函数调用之间的不变
    return &num; // 返回静态变量的地址
}

int main() {
    
    
    int* result = pointerToFunction(); // 调用指针函数,得到整数指针
    printf("Value at the pointer: %d\n", *result); // 打印指针所指向的值

    return 0;
}

the difference:

The pointer function returns an address in the function, and it uses the pointer to receive it.
The pointer function refers to the pointer pointing to this function. You can directly use this pointer to operate this function, which means that it can be assigned a value.

函数指针
int (*functionPtr)(int, int); // 声明一个函数指针
functionPtr = add;           // 将函数指针指向一个函数
int result = functionPtr(5, 3); // 使用函数指针调用函数

Off topic, references in C++

This reference is actually equivalent to creating a new name. Directly change the original data through operations inside the function:

Function parameter passing

void modifyValue(int &x) {
    
    
    x = 42;
}

int main() {
    
    
    int num = 10;
    modifyValue(num); // 通过引用传递参数
    // 现在,num 的值已经变为 42,直接在函数内部把num的值改了
    return 0;
}

Returning references: Functions can return references to allow chaining of operations, and variables outside the function can be modified.

int &getSomeValue() {
    
    
    static int x = 42;
    return x;
}

int main() {
    
    
    int &ref = getSomeValue(); // 返回静态变量的引用
    ref = 99; // 修改静态变量的值
    // 现在,静态变量的值为 99
    return 0;
}

Note that the reference must be initialized when declared, and once initialized, it will always refer to the same object and cannot be re-bound to another object. References are typically used to pass and manipulate variables rather than create new variables.

Guess you like

Origin blog.csdn.net/qq_38156743/article/details/132830635