我试图使用实现在回答这个问题的安德建议的LSB查找方法:Position of least significant bit that is set演员INT在C字节数组++
然而,它导致分段错误。这是一个小程序,演示问题:
#include <iostream>
typedef unsigned char Byte;
int main()
{
int value = 300;
Byte* byteArray = (Byte*)value;
if (byteArray[0] > 0)
{
std::cout<< "This line is never reached. Trying to access the array index results in a seg-fault." << std::endl;
}
return 0;
}
我在做什么错?
我读过,在C++中使用'C-Style'强制转换不是很好的做法。我应该用reinterpret_cast<Byte*>(value)
代替吗?不过,这仍然会导致分段错误。
啊,成功了。谢谢。 – 2011-03-09 18:48:25
修复了他的指针问题,但对于给定的问题,演员仍然是不必要的。 – 2011-03-09 19:09:21