c – 反向传递参数的函数

前端之家收集整理的这篇文章主要介绍了c – 反向传递参数的函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的功能
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++]);

这是我期望的输出

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]);未定义..

原文链接:https://www.f2er.com/c/118950.html

猜你在找的C&C++相关文章