2016-08-03 54 views
0

我有int64_t value1; int32_t value2;。然後,我有*(int32_t*)&value1 = value2;難投用的Int64和INT32指針

能否請你描述一下*(int32_t*)&value1是什麼意思?它是否意味着「value1的最低32位」,因此*(int32_t*)&value1 = value2;意味着「value1的前32位被替換爲value2的前32位」?

還是我完全錯了嗎?

+0

這是關於字節序。之前有太多關於這方面的問題 –

回答

0

這將依賴機器的字節順序在其上運行。這是非常糟糕的做法。 在一個小的endian系統上,這是大多數Intel硬件,最低位的字節是第一位,因此32位SIGNED值將寫入64位整數的低32位。在一個大的endian系統中,它將被寫入高位。

注意,INT簽署。如果value2是負值,則生成的64位數不會爲負數(除非它已爲負數)。

它也不會改變64位整數的高位。

我說...不這樣做呢?

編輯 爲了更直接地回答你的問題,是的,你是對的,這取決於你的意思是「前32位」。首先以平臺使用的任何順序,是的。

&value1 -> will give the address of value1 
(int32_t*)&value1 -> tells the compiler to treat the address of value1 as a pointer to an int32_t 
*(int32_t*)&value1 -> then dereference the pointer, so assigning to this will put the assigned value into the address of value1 as if it were an int32_t.