Steps for performing complex expressions

Source: Internet
Author: User
Tags compact

Recently wandering on the csdn, I noticed a post:

((*strdest++=*strsrc++)!= ' "); Where predecessors can explain the implementation of the steps in the specific?

For such expressions, we usually have three different views:

1. This style of writing is not only error free (of course there are no bugs), but also a compact formulation.

2. This type of writing is not wrong, but not intuitive enough to understand a bit of trouble, may also lead to understanding errors.

3. There is an undefined place in this formulation, and the result may be wrong.

Roughly speaking, these three kinds of statements have a point. I immediately have the inquisitive interest, want to have an in-depth analysis of this problem. For this combination of expressions, we should grasp two key concepts at the time of Analysis: priority (precedence) and correlation (associativity).

1. Priority level (precedence). The precedence determines that the values of those expressions are evaluated first, and the values of those expressions are evaluated. Typically, the value of the higher-priority expression is evaluated first, and then the evaluated results are evaluated to evaluate the lower-priority expressions. So if we turn our priorities back, we're wrong to evaluate the results.

2. (associativity). For two-mesh expressions, the correlation determines whether the left expression or the right expression is evaluated first, and the evaluated results are then used to evaluate the other expression.

To catch up on these two key points, we should also distinguish what is the value of the expression and what is the value of the variable. When we evaluate an expression, we are interested in the value of the expression, not the value of some of the variables that make up the expression. In many cases, the value of an expression is consistent with the value of some variables, so it is easy to confuse the value of an expression with the value of a variable. You know, in some cases, the value of an expression is not the same as the value of some variables.

With the above theory to arm us, the analysis of the expression seems to be more comfortable:

1. It is obvious that the expression above is a combination of expressions. The combination expression consists of a child expression, which may be a combined expression, thus forming a tree-like data structure. An evaluation of an expression is similar to the traversal of a tree node. First we should notice the "()" operator, which has the highest priority, so the whole expression should be a "!=" operation as a whole. The left side of "!=" is a combined expression, and the right side is a constant "", and obviously the next job is to evaluate (*strdest++=*strsrc++).

2. In this step, we will evaluate the expression (*strdest++=*strsrc++). Because the assignment expression has a lower precedence, the expression can also be written as: (*strdest++) = (*strsrc++), so the entire expression is an "=" operation, and the left side of "=" is a combined expression, and the right is a combined expression, Here it is necessary to determine from the relevance of the left or right is also evaluated. Because the association of "=" is from right to left, the (*strsrc++) is evaluated first (*strdest++).

2.1 In this step, we will evaluate the expression (*strsrc++). Because the priority of "+ +" is greater than "*", the expression can also be written as: * (strsrc++). We first evaluate the expression strsrc++ and then evaluate the value of * (strsrc++) with the value of the expression. For an expression strsrc++, here you need to be aware of the value of the variable and the values of the expression. For the "1" expression, the value of the expression is the value of the variable strsrc, and then the value of the variable strsrc "plus 1", that is, the value of the expression is strsrc before the change, and the STRSRC value changes. It's worth noting that we know that the value of STRSRC will change, but we do not know the exact time when the value of the STRSRC changes, and the specific execution time is determined by the compiler, which determines that the value of any expression dependent on the strsrc is indeterminate, and the value depends on the compiler's implementation. After the evaluation of the strsrc++ is completed, the value operator evaluates the memory space corresponding to the value of the expression.

2.2 In this step, we will evaluate the expression (*strdest++). The analysis of the specific assessment is consistent with the analysis in 2.1.

2.3 In this step, we evaluate the expression (*strdest++) = (*strsrc++), which is an assignment expression that assigns the value of the right expression to the value of the left expression. It is important to note that for an assignment expression, the value of the expression itself equals the value of the left-side subexpression.

3. Because the value of the subexpression on the left side of the "!=" expression has been evaluated, the following "!=" action is performed. The "!=" expression is a Boolean value.

Through the above in-depth analysis, we know that this expression completes the following functions:

1. For pointer strdest, STRSRC, assigns the value of the memory space that STRSRC refers to to the memory space referred to by strdest.

2. Determine whether the assigned value of the strdest refers to the amount of memory space is equal to 0.

3. For pointers strdest,strsrc, their values are added to 1, which means the next element.

We can see that an expression accomplishes three functions, and the expression is literally "quite compact". And the value of this expression can be determined, because all the analysis is based on the C standard. This is true for the ability to use such code in the Code of Practice, and the key point is to follow the code specification for the project.

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.