Detailed explanation of function pointers in C language

function pointer

In the C language, a pointer function is a function that returns a pointer. Specifically, the pointer function returns a pointer to a specific type of data, which can be an array, a structure, a function, etc.

definition:

return type(*function name)(parameter list)

Wherein, the return type is the data type pointed to by the pointer, the function name is the name of the pointer function, and the parameter list is the parameter list accepted by the pointer function, which can be empty.

The following is an example of a function returning a pointer to int type data:

int *max(int *a, int *b) {
    
    
    if (*a > *b) {
    
    
        return a;
    } else {
    
    
        return b;
    }
}

In the above code, we define a pointer function named max, which accepts two pointers a and b of type int as parameters, and returns a pointer to data of type int. Inside the function, we compare the data pointed by the pointer a and the pointer b, if the data pointed by a is greater than the data pointed by b, return the pointer a, otherwise return the pointer b.

Here is an example using pointer functions:

int main() {
    
    
    int a = 1, b = 2;
    int *p = max(&a, &b);
    printf("The maximum value is %d\n", *p);
    return 0;
}

In the above code, we define two variables a and b of int type, and then call the pointer function max, passing their addresses as parameters. The pointer function returns a pointer to the larger of a and b, which we assign to the pointer p, dereferences it, and prints the data it points to.

It should be noted that the pointer returned by the pointer function can point to global variables, static variables, data allocated by dynamic memory, etc., but should not return pointers to local variables, because after the function returns, the memory space where the local variables are located will be released , the pointer to it becomes an invalid pointer, which leads to undefined behavior and program crashes.

return void function pointer

Here is an example of a pointer function returning an invalid pointer:

#include <stdio.h>

int *get_array() {
    
    
    int arr[3] = {
    
    1, 2, 3};
    return arr;
}

int main() {
    
    
    int *ptr = get_array();
    printf("%d\n", *ptr);
    return 0;
}

In the above code, we defined a pointer function called get_array, which returns a pointer to int type data. Inside the function, we define an array arr of type int with length 3 and return it as a pointer. However, since arr is a local variable defined inside the function, after the function returns, the memory space where arr is located will be released, and the pointer to it will become an invalid pointer.

In the main function, we assign the pointer returned by the pointer function to the pointer variable ptr, and try to output the data pointed to by the pointer. Since the pointer has become an invalid pointer, the program may output undefined results, or cause the program to crash. Therefore, when writing pointer functions, you need to avoid returning pointers to local variables, so as not to cause the pointer to become an invalid pointer.

Solve return invalid pointer

Here's an example that avoids returning an invalid pointer:

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

int *get_array() {
    
    
    int *arr = malloc(3 * sizeof(int));
    if (arr == NULL) {
    
    
        printf("Memory allocation failed.");
        return NULL;
    }
    arr[0] = 1;
    arr[1] = 2;
    arr[2] = 3;
    return arr;
}

int main() {
    
    
    int *ptr = get_array();
    if (ptr == NULL) {
    
    
        return 1;
    }
    printf("%d\n", ptr[0]);
    free(ptr);
    return 0;
}

In the above code, we usemalloc functionAn int type memory space of size 3 is allocated on the heap and returned as a pointer. Since the memory space is allocated on the heap, it will not be released after the function returns, so it is possible to avoid returning an invalid pointer.

In the main function, we assign the pointer returned by the pointer function to the pointer variable ptr, and try to output the data pointed to by the pointer. Since the memory space pointed to by the pointer is valid, the program can correctly output the value of the first element pointed to by ptr. Finally, we usefree functionFree that memory space.

Therefore, when writing a pointer function, you can consider allocating memory space on the heap and returning it as a pointer, so as to avoid returning an invalid pointer. At the same time, the memory space needs to be released in time after use to avoid memory leaks.

Guess you like

Origin blog.csdn.net/weixin_45172119/article/details/129773389