[] - 运算符中的加法和增量表达式

1 c++ iso undefined-behavior c++11

根据标准是以下两个表达式未定义的行为?

int a = 1, b = 1; 
int c[] = { 1, 2, 3, 4, 5 }; 

// Do the following expressions lead to Undefined Behavior ? 
c[a++]; // (1)  
c[a+b]; // (2) 
Run Code Online (Sandbox Code Playgroud)

Sou*_*osh 5

不,以他们目前的形式,他们不会导致不确定的行为.您可能不担心发生序列点违规.

  • 如果在索引中使用c[a++];现有值,aa作为副作用(后增量运算符)递增.
  • c[a+b];没有变量值被更改,并且a+b是数组的有效索引.