微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/c/118950.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐