我試圖使用實現在回答這個問題的安德建議的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