C pointer (4) parse the c Declaration statement

Source: Internet
Author: User

(4) parse the C statement

Before continuing to explore the C pointer, it is necessary to parse the complex declaration syntax in the C language.

You only need to remember two points: one principle and one rule.

Principle: first look at the identifier.

Rule: The operator priority is a rule.


Example

1. The simplest int array [3];

Conclusion: array is an array, array size is 3, and element type is int.

Parsing process: first look at the identifier: array, there is only one operator [], then the array is an array, the element type is int, it is complete.


2. Difficult

(1) array pointer int (* array) [3];

Conclusion: array is a pointer pointing to an array with a size of 3 and an element type int in the array.

Parsing process: first look at the identifier: array. Because the brackets operator is used, it can only be parsed (* array) First, indicating that array is a pointer. If the parsing is complete, you can discard it. Then only [] is left, indicating that it is directed to an array, and the rest is not explained.

(2) pointer array int * array [3];

Conclusion: array is an array with a size of 3 and the element type in the array is int *.

Parsing process: first look at the identifier: array. Because [] has a higher priority than *, array first combines with [], indicating that array is an array. Only one X is left, indicating that the pointer is stored in the array. The Pointer Points to an int type, that is, the element type in the array is int *.

It seems that the difference is only one (), meaning completely different.

Memory of array pointers and pointer Arrays:



3. More complicated

Function pointer int (* pfun) (INT, INT );

Conclusion: pfun is a function pointer, that is, pfun points to a function. This function has two parameters: int and INT, And the return value of the function is int.

Parsing process: first look at the identifier: pfun. Because the brackets operator is used, it can only be parsed first (* pfun). This indicates that pfun is a pointer. Now only (INT, INT) is left. It is further explained that pfun is pointing to a function. This function has two parameters, the type is int, and the final int indicates that the return value type of the function is int.


4. More complex

Int * (* pfun [3]) (INT, void (*) (INT, int *));

Whether you are dazzled or not. Using the above method, you will parse it. You may wish to write it in the comments for a comparison.


Column Directory: C pointer


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.