缺乏邏輯這不是邏輯:C++的指針初始化及使用
int *ptr = &otherInt;
當我們做到這一點,ptr
給otherInt的ADRESS,*ptr
給otherInt。
但是我們明確地寫(* ptr)等於地址*ptr = &otherInt
!
邏輯上,*ptr
應該給出地址,而ptr
則爲其他Int。
你不覺得嗎?
缺乏邏輯這不是邏輯:C++的指針初始化及使用
int *ptr = &otherInt;
當我們做到這一點,ptr
給otherInt的ADRESS,*ptr
給otherInt。
但是我們明確地寫(* ptr)等於地址*ptr = &otherInt
!
邏輯上,*ptr
應該給出地址,而ptr
則爲其他Int。
你不覺得嗎?
我覺得你弄錯了。考慮一下:
int *ptr;
int otherInt = 10;
ptr = &otherInt; // Notice this line.
星號是聲明的一部分,而不是賦值。
是的!這就是爲什麼我更喜歡寫:'int * ptr;' –
但是,如果你在同一行中聲明多個變量,那更令人困惑...... – Kelm
_'if你在一行中聲明多重變量'_這是**強烈不鼓勵**大多數當前相關的C++編碼規則無論如何。 –
http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome –