c++ - 函数反向传递参数

标签 c++ function reverse operator-precedence

这是我的功能:

void abc(char  *def, unsigned int w, unsigned int x, unsigned int y, unsigned int z)
{
   printf("val 1 : %d\n", w);
   printf("val 2 : %d\n", x);
   printf("val 3 : %d\n", y);
   printf("val 4 : %d\n", z);
}

这里是我调用这个函数的地方:

unsigned int exp[4] = { 1, 2, 3, 4 };
unsigned short count = 0;
abc(anyarray, exp[count++], exp[count++], exp[count++], exp[count++]);

这是我期望的输出:

val1 : 1
val2 : 2
val3 : 3
val4 : 4

但我得到的是完全相反的:

val1 : 4
val2 : 3
val3 : 2
val4 : 1

不知道为什么?任何帮助将不胜感激。

最佳答案

来自标准文档,5.4

Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified58) Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored. The requirements of this paragraph shall be met for each allowable ordering of the subexpressions of a full expression; otherwise the behavior is undefined.

来自标准文档本身的示例,

i = v[i++];//行为未定义

也是出于同样的原因

abc(anyarray, exp[count++], exp[count++], exp[count++], exp[count++]); 未定义..

关于c++ - 函数反向传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3844570/

相关文章:

javascript - 为什么 bind() 不适用于对象内部另一个函数内的函数?

事件上的 JavaScript 函数在声明上运行

c++ - C++ 中的迭代器和模板

c++ - 构建 Qt-Static 5.3.2 时遇到问题

c++ - 提高 SQLite SELECT 性能

c - 通过引用传递给递归函数

c++ - 避免非常量引用参数

c - 为什么我得到错误的输出,我该如何解决这个问题?

c - 如何反转我的 C 列表?

java - 我们如何在不使用任何内置函数的情况下在 Java 中找到 String 的长度? (甚至不是 charAt() 或 toCharArray() 或 length())