106 only need a recruit, thoroughly conquer C language pointer

Source: Internet
Author: User

C Learning Network: Link->C Learning Network

C Training Network: Link->C Training Network before we explained the pointer array, two-dimensional array pointers, function pointers, and other more complex pointers, they are defined in the form of:

int *p1[6]; pointer array int * (p2[6]); Pointer array, and the form above is equivalent int (*P3) [6]; Two-dimensional array pointer int (*P4) (int, int); function pointers I believe that most beginners are very confused about the above several forms of pointers, do not know where to start to understand, why P1, p2 is an array, and P3 is a pointer, they are just a bracket difference.

The pointer is the most powerful and flexible part of C language, is also the most difficult to understand the part, it is the focus of learning C language, without learning the pointer can not talk about learning C language. If you feel that the above several forms of pointers can be reluctantly accepted, then the following two pointers are not maddening.
char * (* c[10]) (int **p); Int (* (* (*PFUNC) (int *)) [5]) (int *); As long as we find the tip, and the complicated pointer is understandable, we're going to prick the window paper.

The C language standard stipulates that for a definition of a symbol, the compiler always reads from its name and then resolves it in order of precedence. Yes, starting with the name, not from the beginning or from the end, is the key to understanding the complex pointer.

For beginners, the precedence of several operators is very confusing, and their precedence from highest to lowest is the part of the definition that is enclosed in parentheses (). Suffix operator: bracket () means that this is a function, and the square brackets [] indicate that this is an array. Prefix operator: asterisk * denotes "pointer to xxx".
Learned the "lore", then we will be more than one, one by one to break the pointer definition above. 1) int *p1[6];Starting from P1, it has a * on the left and a priority of [],[] higher than * on the right, so the compiler resolves P1[6],P1 first a 6-element array and then the int *, which is used to illustrate the type of the array element. As a whole, P1 is an array of 6 int * Elements, also known as an array of pointers. 2) int (*P3) [6];Starting with P3, () is the highest priority, the compiler first resolves (*p3), P3 First is a pointer, and the remaining int [6] is the type of data that P3 points to, and it is a one-dimensional array of 6 elements. As a whole, P3 is a pointer to an array of 6 int elements, also known as a two-dimensional array pointer. To be able to traverse the array element through the pointer, when defining array pointers, you need to do dimensionality reduction, for example, the data type that the three-dimensional array pointer actually points to is a two-dimensional array, and the two-dimensional array pointer actually points to a one-dimensional array, and the one-dimensional array pointer actually points to a basic type; in an expression, The array name also converts the same (descending one dimension). 3 int (*P4) (int, int);Starting from P4, () is the highest priority, the compiler first resolves (*p4), P4 first is a pointer, and the () description P4 points to a function, an int in parentheses, an int is a list of arguments, and an int at the beginning is used to describe the return value type of the function. Overall, P4 is a pointer to a function that points to the prototype as an int func (int, int); 4) char * (* c[10]) (int **p);This definition has two names, namely C and P, at first glance P is the name of the pointer variable, but unfortunately it is wrong. If p is a pointer variable name, C[10] This type of writing defines a new name, which is inconceivable.

Take c as the variable's name, first look inside the bracket (green bold):

char * (* c[10]) (int **p); [] has precedence over *, the compiler first resolves c[10],c first is an array, and the previous * indicates that each array element is a pointer, but does not yet know what type of data to point to. Overall, (* c[10]) indicates that C is an array of pointers, except that the data type that the pointer points to is not yet determined.

Jump out of parentheses, depending on the precedence rule (() precedence over *) should look to the right (red bold):

char * (* c[10]) (int **p); () description is a function, int **p is a function argument.

Then look to the left (orange bold):

char * (* c[10]) (int **p); char * is the return value type of the function.

On the whole, we can divide the definition into two parts:

char * (* c[10]) (int **p); The green bold indicates that C is an array of pointers, and that red bold indicates the type of data the pointer points to, which is combined: C is an array of pointers with 10 elements, each pointing to a function of a prototype of Char *func (int **p); 5 Int (* (* (*PFUNC) (int *)) [5]) (int *); Starting with the Pfunc, look inside the brackets (green bold):

Int (* (* (*pfunc)(int *)) [5]) (int *); Pfunc is a pointer.

Jump out of brackets and look at both sides of it (red bold):

Int (* (* (*pfunc) (int *)) [5]) (int *); According to the precedence rule, you should first look at the right (int *), which indicates that this is a function, int * is the argument list. Then look at the left side of the *, which indicates that the return value of the function is a pointer, except that the data type that the pointer points to is not determined.

The above two parts are synthesized into a whole, as shown in the blue bold below, which indicates that Pfunc is a pointer to a function, now that the parameter list of the function is determined, and that the return value is a pointer (just don't know what type of data it points to).

Int (* ( * (*pfunc) (int *) ) [5]) (int *); A section of bold blue that describes what type of pointer the function returns.

Let's go through the analysis and then jump out of the bracket (red bold):

Int (* (* (*pfunc) (int *)) [5]) (int *); [] has precedence over *, first look to the right, [5] means this is an array, and then to the left, * indicates that each element of the array is a pointer. In other words, * [5] is an array of pointers, and the function returns a pointer to such an array.

So, what type of data does the pointer in the pointer array point to? And then jump out of the bracket (orange bold):

Int (* (* ( *pfunc) (int *) ) [5]) (int *); First look at the orange part of the right, it is a function, and then look to the left, it is the function of the return value type. That is, the pointer in the array of pointers points to the function of the prototype int func (int *).

The top three parts are: Pfunc is a function pointer (the blue part) whose return value is a pointer to an array of pointers (the red part) and a pointer to an int func (int *) in the pointer array, and a function (the orange portion).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.