Left-right rule-complex pointer Parsing

Source: Internet
Author: User

Core tips:All the complex pointer declarations in C language are made up of various declarations nested. How can we interpret complex pointer declarations? The left-right rule is a name-and-use method.

All the complex pointer declarations in C language are made up of various declarations nested. How can we interpret complex pointer declarations? The left-right rule is a name-and-use method. However, the right-left Method

In fact, it is not the content in the C standard. It is a method summarized from the C standard statement. The C standard declaration rule is used to solve how to create a declaration, while the right-left rule is used

To solve how to identify a statement, the two can be said to be the opposite. The original English version of the right-left rule is as follows:

The right-left rule: Start reading the declaration from the innermost parentheses, go right, and then go left. When you

Encounter parentheses, the direction shoshould be reversed. Once everything in the parentheses has been

Parsed, jump out of it. Continue till the whole declaration has been parsed.

The English translation is as follows:

Right left rule: first, start from the parentheses in the innermost part, and then look to the right, then to the left. When parentheses are encountered, the reading direction should be dropped. Once everything in the parentheses is parsed, the parentheses appear. Repeat this process until the entire declaration is parsed.

I want to make a small correction to this rule. It should be read from undefined identifiers rather than from parentheses. The reason is that there may be multiple identifiers in a declaration, but there is only one undefined identifier.

Now we can use some examples to discuss the application of the right-left rule. First, we will gradually learn more about the website source code:

Int (* func) (int * p );

First, find the undefined identifier, that is, func, which has a pair of parentheses and a * sign on the left. This indicates that func is a pointer, and then jump out of the parentheses. first look at it.

The right side is also a parentheses, which means (* func) is a function, and func is a pointer to this type of function, it is a function pointer, this type of function has an int * type parameter, and the return value type is int.

Int (* func) (int * p, int (* f) (int *));

Func is enclosed by a pair of parentheses, and there is a "*" on the left, which indicates that func is a pointer. It jumps out of the brackets and there is a bracket on the right. Then func is a pointer to the function, this type of function has parameters such as int * and int (*) (int *), and the return value is of the int type. Let's take a look at the func parameter int (* f) (int *). Similar to the previous explanation, f is also a function pointer. the pointer to a function has an int * type parameter and the return value is int.

Int (* func [5]) (int * p );

The right side of func is a [] Operator, indicating that func is an array with five elements, and there is a * on the left of func, indicating that the element of func is a pointer, note that * not modifying func, but modifying func [5] is because the [] operator has a higher priority than *, and func is first combined, therefore, * modifies func [5]. Jump out of this bracket and look at the right. It is also a pair of parentheses, indicating that the element of the func array is a pointer of the function type. It points to the function with an int * type parameter and the return value type is int.

Int (* func) [5]) (int * p );

Func is enclosed by parentheses, and there is a * on the left. func is a pointer, out of parentheses, and a [] operator number on the right, which indicates that func is a pointer to an array, to the left, there is a "*" on the left, indicating that the element of this array is a pointer, and then the brackets appear on the right, indicating that the element of this array is a pointer to the function. To sum up, func is a pointer to an array. The elements of this array are function pointers. These pointers point to functions with int x parameters and return values of the int type.

Int (* func) (int * p) [5];

Func is a function pointer. This type of function has an int * type parameter. The returned value is a pointer to an array. The elements of the array to which it points are arrays with five int elements.

Note that some complex pointer declarations are invalid, for example:

Int func (void) [5];

Func is a function that returns an array with five int elements. However, the function return value in C language cannot be an array. This is because if the function return value is an array, what receives the content of this array must also be an array, however, the array name in C language is a right value, which cannot be used as the left value to receive another array. Therefore, the return value of the function cannot be an array.

Int func [5] (void );

Func is an array with five elements. All elements of this array are functions. This is also invalid because the elements of the array must be of the same type and the memory space occupied by each element must be the same. Obviously, the function cannot meet this requirement, even if the function type is the same, the space occupied by the function is usually different.

As an exercise, the following lists several complex pointer declarations for readers to resolve themselves.

Int (* func) [5] [6]) [7] [8];

Int (* func) (int *) [5]) (int *);

Int (* func [7] [8] [9]) (int *) [5];

In reality, when a complex pointer needs to be declared, if the entire declaration is written in the form shown above, the readability of the program is greatly impaired. Typedef should be used to declare layers by Layer

Break down to enhance readability. For example, for declaration:

Int (* func) (int * p) [5];

It can be decomposed as follows:

Typedef int (* PARA) [5];

Typedef PARA (* func) (int *);

This makes it easier to see.

The answer is as follows:

Int (* func) [5] [6]) [7] [8];

Func is a pointer to an array. The elements of this array are two-dimensional arrays with 5x6 int elements, and the elements of this two-dimensional array are two-dimensional arrays.

Typedef int (* PARA) [7] [8];

Typedef PARA (* func) [5] [6];

Int (* func) (int *) [5]) (int *);

Func is a function pointer. the return value of such functions is a pointer to an array, and the elements pointing to the array are also function pointers. The function that points to has an int x parameter and the return value is int.

Typedef int (* PARA1) (int *);

Typedef PARA1 (* PARA2) [5];

Typedef PARA2 (* func) (int *);

Int (* func [7] [8] [9]) (int *) [5];

Func is an array. The element of this array is a function pointer. This type of function has an int * parameter. The returned value is a pointer to an array, and the element pointing to the array has five int elements.

.

Typedef int (* PARA1) [5];

Typedef PARA1 (* PARA2) (int *);

Typedef PARA2 func [7] [8] [9];

Source: http://www.codesocang.com/jiaocheng/Cyuyan/2013/0922/6066.html

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.