Advanced C language declaration Learning

Source: Internet
Author: User

Author:Yang Shuo, a lecturer at Huaqing vision embedded College.

The design philosophy of C language requires that the object declaration form should be as similar as its use form as possible. For example, an int type pointer array should be declared as int * P [3]; and use an expression like * P [I] to reference or use the int data pointed to by the pointer. Therefore, its declaration form is very similar to the usage form. The advantage of doing so is that the priority of different operators is the same in "Declaration" and "use, the disadvantage lies in the fact that the priority of operators in the C language is too complex (15 or more, depending on how you calculate it). This is an improper and overly complicated design of the C language.
In fact, some keywords can only appear in the declaration, rather than in use, such as volatile and const, which makes the declaration form and use form have fewer and fewer examples of numbers. If you want to forcibly convert something to a pointer to an array, you have to use the following statement to indicate this forced type conversion:
--- Char (* j) [20];
--- J = (char (*) [20]) malloc (20 );
This forced type conversion looks funny, And the parentheses on both sides of the asterisk seem dispensable, But it turns into an invalid statement if it is removed.
The statements involving pointers and const may have the following combinations:
--- Const int * P;
--- Int const * P;
--- Int * const P;
In the first two cases, the pointer points to an object that is read-only, and in the last case, the pointer is read-only.
If we want to make the object and pointer both read-only, the following two declarations can achieve this:
--- Const int * const P;
--- Int const * const P;

After the preliminary and intermediate articles and the previous studies, we found that analyzing a declaration is to separate the declaration according to the operator priority rules and explain each component separately. To understand a declaration, you must understand the priority rules. The following are the priority rules stated in C Language in Expert C programming:
A declares that it reads data starting from its name and then reads data in order of priority;
The priority of B is from high to low:
B .1 The Section enclosed in parentheses in the statement;
B .2 suffix OPERATOR: brackets () indicate that this is a function, and square brackets [] indicate that this is an array;
B .3 prefix OPERATOR: asterisk * identifies "pointing ...... Pointer ";
C. If the const and (or) Volatile keywords follow the type specifiers (such as int and long), it acts on the type specifiers. In other cases, const and (or) the volatile keyword acts on the pointer asterisk next to its left.
Now, let's use priority rules to analyze a complicated statement in C language:
--- Char * const * (* Next )();
B .1 (* Next) -- next is a point ...... Pointer
B .2 (* Next) () -- next is a function pointer.
B .3 * (* Next) () -- next is a function pointer. This function returns a pointer ...... Pointer
C char * const -- a constant pointer to a character type
Therefore, char * const * (* next) (); indicates that next is a function pointer, which returns a constant pointer to the character type.
I think now, do we find that the analysis of C language statements is not as painful as it was at the beginning, but it brings us pleasure, isn't it? In fact, we only need to analyze several instances and read some high-quality C code, analyzing a c language statement is no longer a terrible thing (What's more, we also have the powerful declaration interpreter cdecl, haha !), Once this wild horse is tame, it will help us Compile High-Quality C code. Are you ready?

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.