Analyze the statement of C language from two examples

Source: Internet
Author: User

 

When reading the third chapter of "C expert programming", the book talked about the C language statement. The book "C expert programming" contains more than two hundred pages, I spent a chapter explaining this issue, which is enough to see the importance of this issue. It is far from enough to fully understand the C language statement, you need to practice more and read a lot of code written by others. The following two examples from expert C programming are used to illustrate this problem. Many of the following content is taken from page 55th-79 of Expert C programming.

In C language, the declaration form is similar to the format used. This usage may be original in C language, and K & R also acknowledges that "the syntax of C language declaration sometimes brings serious problems ". The biggest problem with C language statements is that you cannot read a statement from left to right in a natural way that people are used. The following is an example:

Char * const * (* next )();

If you can see the meaning of this statement at first glance, it proves that your C language skills have reached a certain level. The recognition steps in Expert C programming are as follows:

1) start with the variable name "next" and notice that it is directly enclosed in parentheses;

2) Let's take the things in the brackets as a whole and conclude that "next" is a pointer;

3) consider the things outside the brackets and make a choice between the asterisk prefix and the suffix of the brackets;

4) according to the priority rules declared in C Language (which will be given later), the higher priority is the function brackets on the right, so "next" is a function pointer pointing to a return... functions;

5) then, process the prefix "*" to get the content indicated by the pointer;

6) Finally, interpret "char * const *" as a constant pointer to a string.

To sum up the above results, this statement indicates "next is a pointer pointing to a function. This function returns another pointer pointing to a constant pointer of char type ". This problem was solved.

Let's look at another example:

Char * (* c [10]) (int ** p );

First, start with the variable name c, and then process the suffix "[]", indicating that c is an array, and then process the prefix "*", indicating that c is a pointer array. Then, the parentheses indicate that the pointer type in array c is a pointer to a function, and the parameter of this function has only one: a pointer to a pointer, the return value of this function is a pointer to a string. Summarized as follows:

"C is an array [0... 9]. Its element type is a function pointer, and its return value is a pointer to a string, and a pointer to the pointer is used as a unique parameter ".

The priority rules of C language declarations mentioned in Expert C programming are as follows, from page 64th.

 

 

Author Hai Zi

Related Article

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.