全部博文(230)
分类: C/C++
2008-07-03 22:10:26
不可移植的代码例子:
typedef struct Version {
INT16U Ver;
} CFGFILEVER;
const INT16U RELEASE_VERSION = 0x8801;
CFGFILEVER *pCfgVer = NULL; /* pointer to version */
pTmp = (INT8U *)(CONF_ROM_ADDRESS);
pCfgVer = (CFGFILEVER *)pTmp;
if (RELEASE_VERSION == pCfgVer->Ver) //pCfgVer->Ver的值与cpu大小端有关
{
//...
}
修改后的代码:(可移植)
INT16U wVersion = 0; /* cfg file version */
pTmp = (INT8U *)(CONF_ROM_ADDRESS);
wVersion = MAKEWORD(pTmp[0], pTmp[1]); //明确定义字节顺序,与实际cpu大小端无关
if (RELEASE_VERSION == wVersion)
{
//...
}