Right-left rule of pointer Declaration

Source: Internet
Author: User

All the complex pointer declarations in C language are made up of various declarations nested. How can we interpret complex pointer declarations? The left-hand rule is a well-known and common method. However, the right-left rule is not actually the content in the C standard. It is a method summarized from the C standard statement. The C standard declaration rules are used to solve how to create a declaration, while the right-left rule is used to identify a declaration. The two are 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, starting from the simplest:

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 jumps out of the parentheses, first look at the right, it is also a parentheses, which means (* func) is a function, and func is a pointer to this type of function, 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 * sign 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 side. It is also a circle-enclosed sign, indicating that the element of the func array is a pointer of the function type. The function it points to has an int * type parameter, 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, now, to the left, there is a "*" on the left, indicating that the element of this array is a pointer, and then jumps out of the brackets. There is a bracket 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. The answer is placed in chapter 10.

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 separate declarations layer by layer to enhance readability. For example, for declarations:

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.

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.