Execution steps of complex expressions

Source: Internet
Author: User

I noticed a post about csdn:
(* Strdest ++ = * strsrc ++ )! = '/0'); Where can the predecessors explain the specific steps involved?
For such expressions, we usually have three views:
1. This writing method is not only correct (of course there are no bugs), but also compact.
2. Although there is no error in this writing method, it is not intuitive enough to understand it, which may lead to incorrect understanding.
3. There are undefined places in this write method, and the execution result may be incorrect.
In a rough view, these three statements are all reasonable. I was suddenly interested and wanted to have an in-depth analysis on this issue. For this combination expression, we should grasp two key concepts during analysis: precedence and associativity ).
1. precedence ). The priority determines that the values of the expressions are evaluated first, and those expressions are evaluated later. Generally, the values of expressions with a higher priority are evaluated first, and then evaluated with the evaluation results. Therefore, if we reverse the priority, the evaluation result is incorrect.
2. (associativity ). For a Binary Expression, associativity determine whether the expression on the left or the expression on the right is evaluated first, and the result is then used to evaluate another expression.
While grasping these two key points, we should also distinguish what is the expression value and what is the variable value. When evaluating an expression, we are interested in the value of the expression, rather than the value of some variables that constitute the expression. In many cases, the expression value is the same as the value of some variables, so we can easily confuse the value of the expression with the value of the variable. In some cases, the expression value is not the same as the value of some variables.

With the above theory to arm us, we can easily analyze the expressions:
1. Obviously, the above expression is a combination expression. A combination expression is composed of subexpressions, which may also be a combination expression, forming a tree-like data structure. The evaluation of expressions is similar to the traversal of Tree nodes. First, we should note that the "()" operator has the highest priority, so the whole expression should be "! = "Operation. "! = "There is a combination expression on the left, while a constant on the right"/0'. Obviously, the following job is to evaluate (* strdest ++ = * strsrc ++ ).

2. In this step, we need to evaluate the expression (* strdest ++ = * strsrc ++. Because the value assignment expression has a lower priority, the expression can be written as :( * strdest ++) = (* strsrc ++), so the entire expression is a "=" operation, "=" There is a combination expression on the left, and a combination expression on the right. here we need to judge from the relevance to determine whether the left or right is also evaluated. Because "=" is correlated from right to left, (* strsrc ++) is evaluated first, and (* strdest ++) is evaluated later.

2.1 In this step, we need to evaluate the expression (* strsrc ++. Because "++" has a higher priority than "*", the expression can be written as * (strsrc ++ ). We need to evaluate the expression strsrc ++ first, and then evaluate the value of * (strsrc ++) with the expression value. For the expression strsrc ++, note that the value of the variable and the value of the expression must be distinguished. For the "Add 1" expression, the expression value is the value of the variable strsrc, and then the value of the variable strsrc will "Add 1", that is, the expression value is the value before the strsrc change, the strsrc value will change. It is worth noting that we know that the value of strsrc will change, but we do not know the specific time when the value of strsrc changes. The specific execution time of this change is determined by the compiler, this determines that the value of any strsrc-dependent expression is uncertain, and the specific value depends on the implementation of the compiler. After the strsrc ++ evaluation is completed, the value operator performs the value operation on the memory space corresponding to the expression value.

2.2 In this step, we need to evaluate the expression (* strdest ++. The specific evaluation analysis is exactly the same as the analysis in section 2.1.

2.3 In this step, we need to evaluate the expression (* strdest ++) = (* strsrc ++). This is a value assignment expression, assign the value of the right expression to the value of the Left expression. It is worth noting that for a value assignment expression, the value of the expression itself is equal to the value of the Left subexpression.

3. Because "! = "The value of the subexpression on the left of the expression has been evaluated. Execute the following command "! = "Operation. "! = "The expression is a Boolean value.

Through the above in-depth analysis, we know that this expression has completed the following functions:
1. For the strdest and strsrc pointers, assign the value of the memory space referred to by strsrc to the memory space referred to by strdest.
2. Determine whether the memory space specified by strdest after the assignment is equal to 0.
3. For the strdest and strsrc pointers, their values are respectively added with 1, pointing to the next element.

We can see that an expression has completed three functions, and the expression is indeed "quite compact ". The value of this expression can be determined, because all analyses are based on the C standard. This is wise to know whether such code can be used in practical code. The key point is to follow the code specifications of the project.

PS: I used Google to find a good table about operator priority and relevance in C language. You can download and print it for future search. The address of this table is: http://www.mhatt.aps.anl.gov/dohn/programming/tables/operator_precedence.pdf

At home

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.