爲了自我充實,我正在構建一個小型鏈表庫,這迫使我處理一個我幾乎無法解釋的問題。看看下面的例子,並假定new_item->data
是typedef結構內的空指針:從void指針獲取int的問題
int *data = malloc(sizeof(int));
*data = 42;
MY_LIST *new_item = malloc(sizeof(*new_item));
new_item->data = &data;
// prints what looks like a memory address
printf(
"data: %i\n",
*((int *) new_item->data)
);
//prints 42
int data2 = *((int *) new_item->data);
printf(
"data2: %i\n",
*((int *) data2)
);
至於我(可能是廢棄的)大腦所知道的,在這兩種情況下我使用同樣的鑄造聲明 - *((int *) new_item->data)
- 從它的容器派生整數。然而,當我在printf內部使用它時,我得到了一個內存地址。我對此有什麼誤解?
歡迎來到指針地獄。 –
調整你的_data_賦值爲'new_item-> data = data;' – amdixon
不應該'MY_LIST * new_item = malloc(sizeof(* new_item));'是'MY_LIST * new_item = malloc(sizeof(new_item));'? 'sizeof(new_item)',而不是'sizeof(* new_item)'? –