※ 引述《deepking (wnuiayldh)》之銘言:
: unsigned int * p;
: unsigned int x,y;
: 用sizeof看 p : 8 bytes
: x : 4 bytes
: y : 4 bytes
: 能夠把p拆成兩半存進x,y
: 之後在把x,y組合成p嗎?
先說一下, 這是用指標的寫法, 至於p[0], p[1]哪一個該assign
到x中, 得看endian決定
雖然按照學到的知識, 了解這樣做是可以的. 可是在這種情況下,
用感覺來說, 會有種說不上來奇怪的感覺, 不知道有沒有人跟我
一樣 XDD
int main(int argc, char *argv[])
{
int TestNumber;
int* pInteger = &TestNumber;
unsigned int* p = (unsigned int*)&pInteger;
printf("Sizeof int*: %d\n", sizeof(int*));
printf("Sizeof int: %d\n", sizeof(int));
printf("\n%p\n", pInteger);
printf("\n[%08X]\n", p[0]);
printf("\n[%08X]\n", p[1]);
system("PAUSE");
return 0;
}
32位元可以用short模擬一下 XD
int main(int argc, char *argv[])
{
short TestNumber;
short* pShort = &TestNumber;
unsigned short* p = (unsigned short*)&pShort;
printf("Sizeof short*: %d\n", sizeof(short*)); //4
printf("Sizeof short: %d\n", sizeof(short)); //2
printf("\n%p\n", pShort);
printf("\n[%04X]\n", p[0]);
printf("\n[%04X]\n", p[1]);
system("PAUSE");
return 0;
}
--
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.32.51.239