简体   繁体   English

箭头运算符和后增量的优先级?

[英]Precedence of the arrow operator and post increment?

New to C. C 的新功能。 So I saw this line of code所以我看到了这行代码

system->word[system->index++] = system->next_char;

Is it equivalent to:是否等同于:

system->word[system->index] = system->next_char;
index++;

What is the precedence for post increment?职位增量的优先级是什么? Does it only increment index's value by 1 after all the operations on the line are done executing?在线上的所有操作都执行完成后,它是否只将索引的值增加 1?

Updating system->index is defined as a side effect that is not sequenced (is not specified to come before or after) the other operations in the statement.更新system->index被定义为未排序(未指定在语句中的其他操作之前或之后)的副作用 The update may occur before, during, or after other operations.更新可能发生在其他操作之前、期间或之后。

The fact that it is not sequenced is irrelevant as long as it is not used elsewhere in the statement, because, if it is not used elsewhere, then nothing the statement does can be affected by when the update occurs.只要它没有在语句的其他地方使用,它没有排序的事实就无关紧要,因为如果它没有在其他地方使用,那么语句所做的任何事情都不会受到更新发生时的影响。 (Note that, even if the update to system->index in memory is done before the value is used, the compiler is response for ensuring that the pre-update value is used.) (注意,即使 memory 中的system->index的更新在使用该值之前完成,编译器也会响应确保使用更新前的值。)

If the object being updated were used elsewhere in the statement in an unsequenced way (that is, no rule specifies which comes first, the update or the other use), then the behavior of the program would not be defined by the C standard.如果正在更新的 object 在语句的其他地方以无顺序的方式使用(即,没有规则指定先更新还是其他使用),则程序的行为将不会由 C 标准定义。

This is not a consequence of precedence.这不是优先级的结果。 Precedence determines the structure of how expressions are interpreted, not the sequencing of their operations.优先级决定了如何解释表达式的结构,而不是它们操作的顺序。

Not.不是。

system->word[system->index++] = system->next_char;

is equivalent to:相当于:

system->word[system->index] = system->next_char;
system->index++;

as index is a field on a struct pointed to by system .因为indexsystem指向的struct上的一个字段。 In case you have also a scalar variable named index you had had no errors but the wrong variable should have been incremented.如果您还有一个名为index的标量变量,则您没有错误,但应该增加错误的变量。

As a general rule, all the unary operators evaluate first on the right side of the variable, then the ones on the left (the right side operators have higher precedence thatn left side ones) and evaluate from closest to the operand, to farthest.作为一般规则,所有一元运算符首先在变量的右侧进行计算,然后是左侧的运算符(右侧运算符的优先级高于左侧运算符)并从最接近操作数到最远进行计算。 So the operator closest to the operand evaluates first, then the one next in the right... so until there are no more right operators on the right, then the closest on the left side, and so on until there are no more left operators on the left.所以最靠近操作数的运算符首先计算,然后是右边的下一个......所以直到右边没有更多的右运算符,然后是左边最近的运算符,依此类推,直到没有更多的左运算符在左边。

So what happens if we have -x++ ?那么如果我们有-x++会发生什么? The ++ is evaluated first to the value of x and a post increment of the variable is schedule to occur, then the one on the left is evaluated, givin as result the value of x before incrementing, changed of sign, and the variable x gets its value incremented (only incremented, no sign change). ++首先被评估为x的值,并且计划发生变量的后递增,然后评估左侧的值,作为结果给出x递增之前的值,改变符号,以及变量x使其值递增(仅递增,无符号更改)。

Another example: let's compute ++x->field : first of all ->field (the whole thing, the -> arrow and the field name) is considered a right unary operator, given a pointer gets the value in field named field fo structure pointed to by x .另一个例子:让我们计算++x->field :首先->field (整个东西, ->箭头和字段名称)被认为是右一元运算符,给定一个指针获取名为field fo 的字段中的值x指向的结构。 This value is incremented (and stored again in x->field ) and the returned value is the final incremented one.该值递增(并再次存储在x->field )并且返回的值是最终递增的值。

Another final example: let's compute *p++ .最后一个例子:让我们计算*p++ The value of p is scheduled to be post incremented, but it's previous value is retained. p的值计划后递增,但保留其先前的值。 Then the old value of p is used to access the value pointed to by it, giving the original pointed to value.然后p的旧值被用来访问它所指向的值,给原来指向的值。 Finally p is incremented and points to the value next to the value accessed.最后p递增并指向访问值旁边的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM