[問題] 指標變數的小問題

看板C_and_CPP (C/C++)作者 (達達馬蹄)時間15年前 (2011/06/15 17:39), 編輯推噓0(005)
留言5則, 4人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) Dev C++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 下列程式碼中,i的值分配給*pi 那為何在輸出的時候,cout<<pi的值不是&i的值? 而下面是我所知的正確作法 pi=&i 就沒有這個問題 那麼在上段程式碼中,指標變數pi裡面存的是什麼? 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) #include<iostream> using namespace std; int main() { int i,*pi; i = 1; *pi = i; cout<<pi<<endl; cout<<&pi<<endl; cout<<&i<<endl; pi = &i; cout<<pi<<endl; cout<<&i<<endl; system("pause"); return 0; } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.4.192

06/15 17:58, , 1F
一開始的 pi 沒有指向有效空間,這隻程式有問題
06/15 17:58, 1F

06/15 18:16, , 2F
是pi裡面沒有存入位址,而只有值嗎?
06/15 18:16, 2F

06/15 19:38, , 3F
你不能拿一份包裹給郵差 而沒有給地址呀
06/15 19:38, 3F

06/15 22:33, , 4F
*pi=&i
06/15 22:33, 4F

06/15 22:36, , 5F
指標存住址呀
06/15 22:36, 5F
文章代碼(AID): #1D-7t5Ij (C_and_CPP)
文章代碼(AID): #1D-7t5Ij (C_and_CPP)