函数反向传递参数
这是我的函数:
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
我不知道为什么?任何帮助将不胜感激。
Here is my function:
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);
}
and here is where I call this function:
unsigned int exp[4] = { 1, 2, 3, 4 };
unsigned short count = 0;
abc(anyarray, exp[count++], exp[count++], exp[count++], exp[count++]);
and here is the output that I expect:
val1 : 1
val2 : 2
val3 : 3
val4 : 4
but what I get is completely reverse of it:
val1 : 4
val2 : 3
val3 : 2
val4 : 1
I don't know why? Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据标准文档,5.4
标准文档本身的一个示例,
i = v[i ++]; // 行为未定义
其原因与
abc(anyarray, exp[count++], exp[count++], exp[count++], exp[count++]);
未定义..From standard docs, 5.4
An example from the Standard docs itself,
i = v[i ++]; / / the behavior is undefined
And it is for the very same reason that
abc(anyarray, exp[count++], exp[count++], exp[count++], exp[count++]);
is undefined..您不应在同一语句中多次使用 ++ 运算符对同一变量进行操作。未定义执行操作的顺序。
尝试:
You should not use the ++ operator, operating on the same variable, more than once in the same statement. The order in which the operation will be performed is not defined.
Try:
您通过多次修改
count
且没有插入 序列点来调用未定义的行为。You've invoked undefined behavior, by modifying
count
more than once without an intervening sequence point.您正在依赖从左到右评估的参数。您不能对它们的评估顺序做出任何假设。在这种情况下,编译器看起来是从右到左对它们求值。
另外,您可能想要查找序列点,因为您可能不应该以这种方式使用 ++ 运算符。
You are counting on the parameters being evaluated left to right. You can't make any assumptions about the order that they're evaluated. In this case, it looks like the compiler is evaluating them right-to-left.
Also, you may want to look up sequence points, because it may be that you shouldn't use the ++ operator in this way.
abc(anyarray, exp[count++], exp[count++], exp[count++], exp[count++]);
abc
参数的求值顺序为 未指定,但表达式会调用未定义的行为,因为您试图在两个序列点之间多次修改变量count
。此外,在
printf()
中使用不正确的格式说明符也会调用 UB。请确保您在printf()
中使用了正确的格式说明符(即%u
表示unsigned int
)。abc(anyarray, exp[count++], exp[count++], exp[count++], exp[count++]);
The order of evaluation of arguments of
abc
is unspecified but the expression invokes undefined behaviour because you are trying to modify a variablecount
more than once between two sequence points.Furthermore using incorrect format specifier in
printf()
also invokes UB. Please make sure you have used correct format specifiers(i.e%u
forunsigned int
) inprintf()
.你得到这个是因为你根据你的问题调用了
adb(exp,a,b,c,d)
,但是在调用函数d
期间首先被推入堆栈,然后c ,b
相对而言。当您在最后一个参数传递exp[count++]
时,该参数将首先处理以推入堆栈,这意味着先推入 1,然后推入 2,然后推入 3,然后推入 4。在被调用的函数中执行 pop,这样您会得到w =4 x=3 y=2 z=1
就是这样。You got this because you called
adb(exp,a,b,c,d)
according to your problem, but during call of functiond
is pushed first on stack and thenc ,b
relatively. As you passedexp[count++]
at last argument which will process first to push over stack means 1 is pushed first then 2 then 3 then 4. And in called function pop performed so you getw=4 x=3 y=2 z=1
that's it.这是因为调用约定。在 _cdecl 中,c/c++ 程序的默认调用约定(根据 microsoft),参数以相反的顺序在堆栈上传递给函数。因此,参数也按相反的顺序求值。
This is because of the calling convention. In _cdecl, the default calling convention for c/c++ programs (according to microsoft), the parameters are passed on the stack to the function in reverse order. Because of this, the parameters are also evaluated in reverse order.