函数反向传递参数

发布于 2024-09-25 23:26:55 字数 621 浏览 1 评论 0原文

这是我的函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

转身泪倾城 2024-10-02 23:26:55

根据标准文档,5.4

除非另有说明,各个运算符的操作数和各个表达式的子表达式的求值顺序,
并且副作用发生的顺序未指定58) 在上一个和下一个序列点 a 之间
标量对象的存储值最多应通过表达式的求值修改一次。此外,之前的
仅应访问值以确定要存储的值。应满足本段的要求
完整表达式的子表达式的每个允许的排序;否则行为未定义。

标准文档本身的一个示例,

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

其原因与

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

From standard docs, 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.

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..

寂寞花火° 2024-10-02 23:26:55

您不应在同一语句中多次使用 ++ 运算符对同一变量进行操作。未定义执行操作的顺序。

尝试:

abc(anyarray, exp[count], exp[count+1], exp[count+2], exp[count+3]);  
count += 4; 

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:

abc(anyarray, exp[count], exp[count+1], exp[count+2], exp[count+3]);  
count += 4; 
表情可笑 2024-10-02 23:26:55

您通过多次修改 count 且没有插入 序列点来调用未定义的行为

You've invoked undefined behavior, by modifying count more than once without an intervening sequence point.

动次打次papapa 2024-10-02 23:26:55

您正在依赖从左到右评估的参数。您不能对它们的评估顺序做出任何假设。在这种情况下,编译器看起来是从右到左对它们求值。

另外,您可能想要查找序列点,因为您可能不应该以这种方式使用 ++ 运算符。

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.

生寂 2024-10-02 23:26:55

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 variable count 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 for unsigned int) in printf().

看透却不说透 2024-10-02 23:26:55

你得到这个是因为你根据你的问题调用了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 function d is pushed first on stack and then c ,b relatively. As you passed exp[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 get w=4 x=3 y=2 z=1 that's it.

旧情别恋 2024-10-02 23:26:55

这是因为调用约定。在 _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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文