简体   繁体   English

下标运算符的评估顺序

[英]Evaluation Order of Subscript Operator

Does there exist any order for evaluation of expressions in case of an array. 在数组的情况下是否存在用于评估表达式的任何顺序。 If an expression E is of the form E1[E2], where E1 & E2 are also expressions, is the order of evaluation of E1 & E2 fixed ? 如果表达式E的形式为E1 [E2],其中E1和E2也是表达式,则E1和E2的评估顺序是否固定?

Here is my code : 这是我的代码:

#include<stdio.h>
int main(){

    int a[5] = {1,2,3,4,5}; 
    (a + printf("1"))[printf("2")];
    (printf("3"))[a + printf("4")];

    return 0;
}   

It's showing output as: 1243 它显示输出为:1243

I'v compiled it with gcc. 我用gcc编译它。

Thanks. 谢谢。

E1[E2] is equivalent of *(E1 + E2) . E1[E2]相当于*(E1 + E2)

And the Standard tells that "the order of evaluation of subexpressions and the order in which side effects take place are both unspecified". 标准告诉我们“子表达式的评估顺序和副作用发生的顺序都是未指明的”。 So, the order of evaluation of E1[E2] is not fixed. 因此, E1[E2]的评估顺序不固定。

N1256 : N1256

6.5 Expressions 6.5表达式
... ...
3 The grouping of operators and operands is indicated by the syntax. 3语法指示运算符和操作数的分组。 74) Except as specified later (for the function-call () , && , || , ?: , and comma operators), the order of evaluation of subexpressions and the order in which side effects take place are both unspecified. 74)除了后面指定的(对于函数调用()&&||?:和逗号运算符),子表达式的评估顺序和副作用发生的顺序都是未指定的。

So the short answer is "no"; 所以简短的回答是“不”; for an expression like E1[E2] , the order in which the subexpressions E1 and E2 are evaluated is not fixed. 对于像E1[E2]这样的表达式,子表达式E1E2的评估顺序不固定。

I hope you only ask this from curiosity and are not depending on this behaviour for something; 我希望你只是好奇地问这个问题而不是依赖于这种行为;

I don't have a C standard near me, but I don't see why you couldn't instead break up the operations into two separate statements, adding an explicit sequence point. 我附近没有C标准,但我不明白为什么你不能将操作分成两个单独的语句,添加一个显式序列点。 You and other people might not remember what the correct order was supposed to be in the future, creating a maintenance issue. 您和其他人可能不记得将来正确的订单应该是什么,从而产生维护问题。

int * the_array = E1;
the_array[E2];

//or

int the_index = E2;
E1[the_index];

//is much clearer

The evaluation order across the subscript operator is undefined . 下标运算符的评估顺序未定义 Let's give another example that isn't so obfuscated. 让我们举一个不那么混淆的例子。

Issue: Consider the following expression: 问题:请考虑以下表达式:

f() + g() * h()

The precedence is quite clear; 优先级非常明确; multiplication wins out over addition, as demonstrated by the corresponding parse tree: 乘法胜过加法,如相应的解析树所示:

       +
    /     \
   /       \
  f()       *
  /          \
  /           \
 g()           h()

the precedence table only tells us how terms are grouped, not the order in which they are evaluated. 优先级表只告诉我们如何对术语进行分组,而不是它们的评估顺序。 The only way to make the order predictable is to introduce what the C Standard calls sequence points . 使订单可预测的唯一方法是引入C标准调用序列点的内容 For example, the steps: 例如,步骤:

x = g();
x = x * h();
x = x + f();

result in the same precedence as before, but with the functions being called in the guaranteed order g() , h() , and f() . 得到与以前相同的优先级,但是在保证顺序g()h()f()调用函数。

So in your example, you have to introduce sequence points to ensure that your printf statements are executed in the desired order. 因此,在您的示例中,您必须引入序列点以确保以所需顺序执行printf语句。

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

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