C pointer reading skills (33)

        In the C language, pointers are the essence, so we always feel that pointers are the most difficult to understand. It is also one of the knowledge points that must be asked in the written test interview. Today we will take a look at how to read those complicated and difficult pointers.

        In the functions related to reading pointers, we have a law of left and right (this is summed up by Elder Tang). Let's take a look at what it looks like, a> first look at the undefined identifier in the innermost parentheses; b> first look to the right and then left; c> encounter parentheses or square brackets can determine the part type and reverse the direction; d> repeat steps b and c until the end of reading . So let's take the code as an example to analyze

#include <stdio.h>

intmain()
    int (*p)(int);

    int (*p1)(int*, int (*f)(int*));
    
    int (*p2[5])(int*);
    
    int (*(*p3)[5])(int*);

    int*(*(*p4)(int*))(int*);
    
    int (*(*p5)(int*))[5];

    return 0;
}

        Then we see that there are a lot of complex functions defined above, let's analyze them one by one

int (*p)(int);
==> p is a pointer, pointing to a function, the pointed function has an int parameter, and the return value is an int

int (*p1)(int*, int (*f)(int*));
==> p1 is a function pointer, the pointed function has int*, f is the second parameter, it is a function pointer, the pointed function parameter is int*, the return value is int;;; the return value type is int

int (*p2[5])(int*);
==> p2 is an array with 5 elements, each element is a pointer, points to a function, and the type is int (int*)

int (*(*p3)[5])(int*);
==> p3 is a pointer, an array pointer, the pointed array has 5 elements, these 5 elements are pointers, which are function pointers, and the pointed function type is int (int*)

int*(*(*p4)(int*))(int*);
==> p4 is a pointer, a function pointer, the parameter is int*, the return value is a pointer, it is a function pointer, and the pointed function type is int*(int*)

int (*(*p5)(int*))[5];
==> p5 is a pointer, a function pointer, the parameter is int*, the return value is a pointer, points to an array, and the pointed array type is int[5]

        We see that after analysis, the function is not difficult, but its readability is very poor. We can rename the last one above using typedef instead, as follows

typedef int(ArrayType)[5];
typedef ArrayType*(FuncType)(int*);

FuncType* p5;

        After renaming, we see that p5 is a pointer , the type of this pointer is ArrayType*(int*) ; and the type of ArrayType is int[5] . Did we improve readability all of a sudden?

        Then the law of left and right is summarized in the process of the compiler's parsing of pointer variables. The meaning of pointer reading exercises is to understand the combined definition of pointers. We can simplify the definition of complex pointers through typedef .


        Welcome to learn C language together , you can add me QQ: 243343083 .

Guess you like

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