2014-02-28 73 views
-4

缺乏邏輯這不是邏輯:C++的指針初始化及使用

int *ptr = &otherInt; 

當我們做到這一點,ptr給otherInt的ADRESS,*ptr給otherInt。

但是我們明確地寫(* ptr)等於地址*ptr = &otherInt

邏輯上,*ptr應該給出地址,而ptr則爲其他Int。

你不覺得嗎?

+0

http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-c​​an-be-done-to-overcome –

回答

1

我覺得你弄錯了。考慮一下:

int *ptr; 
int otherInt = 10; 

ptr = &otherInt; // Notice this line. 

星號是聲明的一部分,而不是賦值。

+0

是的!這就是爲什麼我更喜歡寫:'int * ptr;' –

+0

但是,如果你在同一行中聲明多個變量,那更令人困惑...... – Kelm

+0

_'if你在一行中聲明多重變量'_這是**強烈不鼓勵**大多數當前相關的C++編碼規則無論如何。 –