In-depth analysis of C language statements

Source: Internet
Author: User

If the C language statement is very simple, it is not a good person or a beginner. This article describes some basic content of C language statements. For more information, see C expert programming. First, it is introduced by a simple question. Do you know the difference between int * P [5] and INT (* P) [5? Cover the answer below and think about it. Maybe you know that one is a pointer array containing five pointer elements pointing to an integer, and the other is a pointer to an array containing five integer elements?

Int * P [5] is an array containing five integer pointers. INT (* P) [5] is an array pointer pointing to five integer elements. If you can tell clearly, congratulations, you are at least an entry level or above. What about this? char * const * (* Next )()?

 

If you can clearly describe the above statement, you don't need to read it. The following is all nonsense for you.

 

First, we need to understand the priority rules declared in C language, as follows:

A declares that it reads data starting from its name and then reads data in order of priority.

The priority of B is:

B .1 The Section enclosed in brackets in life

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 * Indicates pointer...

C. If const acts on the variable pointed to by the pointer before the asterisk, if const acts on the pointer after the asterisk

Another argument is that const modifies the type before it. If there is no type before it, it modifies the type following it.

For example, const int * A; int const * A; int * const A; before the first two const values, the variables pointed to by the pointer are read-only, and the last one is the read-only pointer.

 

Let's start parsing the above statement.

Explanation of applicable rules

A first, check the variable name "Next" and notice that it is directly enclosed by brackets.

B .1 so we should take the content in the brackets as a whole and get "next is a pointer"

B. Consider the things outside the brackets and select between the asterisk prefix and the suffix of the brackets.

The B .2 rule tells us that the higher priority is the function brackets on the right, so it is concluded that "next is a function pointer pointing to a function that returns"

B .3 then, process the prefix "*" to get the content indicated by the pointer

C. Finally, interpret "char * const" as a constant pointer to a character.

 

So to sum up, the real meaning of this formula is: Next is a pointer, which points to a function, and the function returns another pointer, which points to a constant pointer of the char type.

 

 

In fact, the most basic priority is the priority of operators to be understood:

Operator associativity

() []->. Left to right

! -++ -- +-* & Sizeof right to left

*/% Left to right

+-Left to right

<> Left to right

<<=>> = Left to right

=! = Left to right

& Left to right

^ Left to right

| Left to right

& Left-to-right

| Left to right

? : Right to left

= + =-= * =/= % = ^ = |=<<=>> = Right to left

, Left to right

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.