Interpreting Complex declarators

Source: Internet
Author: User

When interpreting complex declarators, square brackets and parentheses (that is, the modifier to the right of the identifier) take precedence over the asterisk (that is, the modifier to the left of the identifier). Brackets and parentheses have the same precedence and are all left-to-right associative. After the declarator is fully interpreted, the type description utilises is applied as the last step. By using parentheses, you can override the default association order and enforce specific interpretations. However, never use parentheses on either side of the identifier name alone. This may be interpreted incorrectly as a parameter list.

An easy way to interpret complex declarators is to read them "from inside to outside" in the following 4 steps:

Start with an identifier and look directly to the right of the square brackets or parentheses (if any).

Explain these square brackets or parentheses, and then look for the left side of the asterisk.

If you encounter a closing parenthesis at any stage, return and apply rules 1 and 2 to all content inside the parentheses.

Apply the type specifier.

View Copy to Clipboard char * (* (*var) ()) in color-coded format [10];
^   ^  ^ ^ ^   ^    ^
7 6 4 2 1 3 5
char * (* (*var) ()) [10];
^   ^  ^ ^ ^   ^    ^
7 6 4 2 1 3 5

In this example, the steps are numbered sequentially and can be interpreted as follows:

The identifier Var is declared as

Pointers to the following content

Functions that return the following content

Pointers to the following content

An array that contains 10 elements, respectively, that are

Pointers to the following content

The char value.

Example
--------------------------------------------------------------------------------

The following example illustrates other complex declarations and demonstrates how parentheses affect the meaning of a declaration.

View Copy to clipboard int *var[5] in color-coded format; /* Array of pointers to int values */
int *var[5]; /* Array of pointers to int values */

The array modifier takes precedence over the pointer modifier, so VAR is declared as an array. The pointer modifier is applied to the type of the array element, so the array element is a pointer to an int value.

View Copy to clipboard int (*var) in color-coded format [5]; /* Pointer to array of int values */
int (*var) [5]; /* Pointer to array of int values */

In this declaration of VAR, the parentheses are given precedence over the array modifier by the pointer modifier, and VAR is declared as a pointer to an array that contains 5 int values.

View Copy to clipboard long *var (long, long) in a color-coded format; /* Function returning pointer to long */
Long *var (long, long); /* Function returning pointer to long */

The function modifier has a higher precedence than the pointer modifier, so this declaration of var declares var as a function that returns a pointer to a long value. The function is declared as a parameter with two long values.

View Copy to Clipboard long (long, long) in a color-sensitive format *var; /* Pointer to function returning long */
Long (*var) (long, long); /* Pointer to function returning long */

This example is similar to the previous example. Parentheses give the pointer modifier precedence over the function modifier, and VAR is declared as a pointer to a function that returns a Long value. Similarly, the function takes two long arguments.

View Copy to clipboard struct both/* Array of pointers to functions */In color-coded format
{/* Returning structures */
int A;
Char b;
} (*var[5]) (struct both, struct both);
struct both/* Array of pointers to functions */
{/* Returning structures */
int A;
Char b;
} (*var[5]) (struct both, struct both);

The element of an array cannot be a function, but this declaration demonstrates how to declare an array of pointers as functions. In this example, VAR is declared as an array that contains 5 pointers to functions that return a structure with two members. The parameters of a function are declared as two structures with the same struct type both. Note that *var[5] requires parentheses on both sides. Without them, the declaration would be an illegal attempt to declare an array of functions, as follows:

View Copy to Clipboard//illegal in color-coded format */
struct both *var[5] (struct both, struct both);
/* Illegal */
struct both *var[5] (struct both, struct both);

The following statement declares an array of pointers.

View copied to clipboard unsigned int * (* const *NAME[5][10]) in color-coded format (void);
unsigned int * (* const *NAME[5][10]) (void);

The name array has 50 elements that are organized in a multidimensional array. These elements are pointers to constant pointers. This constant pointer points to a function that has no arguments and returns pointers to unsigned types.

The next example is a function that returns a pointer to an array containing three double values.

View Copy to Clipboard double (*var (double (*) [3]) in color-coded format [3];
Double (*var (double (*) [3])) [3];

In this declaration, the function returns a pointer to the array because the function that returns the array is illegal. At this point, Var is declared as a function that returns a pointer to an array containing three double values. The function var takes one parameter. A parameter, such as a return value, is a pointer to an array that contains three double values. The parameter type is given by a complex abstract-declarator. Parentheses are required on both sides of the asterisk in the parameter type, and if there is no parentheses, the parameter type is an array of three pointers to double values. For discussion and examples of abstract declarators, see abstract declarators.

View Copy to Clipboard in color-coded format Union sign/* Array of arrays of pointers */
{/* To pointers to unions */
int x;
Unsigned y;
} **var[5][5];
Union sign/* Array of arrays of pointers */
{/* To pointers to unions */
int x;
Unsigned y;
} **var[5][5];

As the example above shows, the pointer can point to another pointer, and the array can contain an array as an element. The Var here is an array of five elements. Each element is an array of five-element pointers that point to a pointer to a union with two members.

View Copy to Clipboard in color-coded format Union sign * (*var[5]) [5]; /* Array of pointers to arrays
of pointers to unions */
Union sign * (*var[5]) [5]; /* Array of pointers to arrays
of pointers to unions */

This example shows how the placement of parentheses changes the meaning of the Declaration. In this example, Var is an array of five-element pointers that point to the Union's five-element pointer array. For an example of how to avoid complex claims using TypeDef, see typedef declarations.

Please see
--------------------------------------------------------------------------------

Concept
Declarations and types
©2015 Microsoft Corporation. All rights reserved.

Send feedback about this topic to Microsoft.

Interpreting Complex declarators

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.