C ++ complex statement

Source: Internet
Author: User

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 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 "*" 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.

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.