读书人

为啥这两个方法明明不同返回值确实相

发布时间: 2013-08-04 18:26:16 作者: rapoo

为什么这两个方法明明不同,返回值确实相同的呢?
void uint2bytes0(byte* out_pArrBytes, uint in_uiValue)
{
out_pArrBytes[0] = (byte)(in_uiValue & 0xff);
out_pArrBytes[1] = (byte)((in_uiValue >> 8) & 0xff);
out_pArrBytes[2] = (byte)((in_uiValue >> 16) & 0xff);
out_pArrBytes[3] = (byte)((in_uiValue >> 24) & 0xff);
}

void uint2bytes1(byte* out_pArrBytes, uint in_uiValue)
{
out_pArrBytes[0] = (byte)(in_uiValue & 0xff);
out_pArrBytes[1] = (byte)((in_uiValue >> 8) & 0xff00);
out_pArrBytes[2] = (byte)((in_uiValue >> 16) & 0xff0000);
out_pArrBytes[3] = (byte)((in_uiValue >> 24) & 0xff000000);
}

坐等高手~ c++
[解决办法]

引用:
Quote: 引用:

我可以很负责任地说,楼主没有用随机大量输入参数进行测试。或者楼主所谓的输出结果一样就是out_pArrBytes[0]一样。byte*变量,在调试器查看只能看到第一个值。

uint2bytes0(bytes, 1434634634535);
echoBytes(bytes);

uint2bytes1(bytes, 1434634634535);
echoBytes(bytes);

void echoBytes(byte* in_pArrBytes)
{
printf("echoBytes: ");
for (int i = 0; i < 4; i ++)
{
printf("%x", *in_pArrBytes + i);
}
printf("\n");
}

echoBytes: 2728292a
echoBytes: 2728292a

这数够大么?

你的输出搞错了,

*in_pArrBytes + i

这句等同于 in_pArrBytes[0] + i,一直只看的第一个元素,后者在两个函数中都对应的这句,所以没有区别。
out_pArrBytes[0] = (byte)(in_uiValue & 0xff);


还是老老实实的用 in_pArrBytes[i] 吧,没事玩什么指针,很 cool 吗?
[解决办法]
*in_pArrBytes + i
-----------------
应该是*(in_pArrBytes + i)
这样2个函数结果就有区别了

读书人网 >C++

热点推荐