[問題] 指標的練習問題

看板C_and_CPP作者 (老頭)時間11年前 (2015/02/16 09:37), 編輯推噓3(302)
留言5則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) dev c++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 指標產生的數字不是正確的 我複製範例的程式碼在我的電腦執行也一樣錯誤 餵入的資料(Input): 預期的正確結果(Expected Output): 變數n=11 指標np=n=11 t也是11喔 錯誤結果(Wrong Output): 變數n=11 指標np=n=2686788 t也是11喔 程式碼(Code):(請善用置底文網頁, 記得排版) #include <iostream> using namespace std; int main(void){ //宣告整數變數n int n=11; printf("變數n=%d\n",n) ; //宣告指標np int *np = &n ; printf("指標np=n=%d\n",np) ; //宣告t int t=*np; printf("t也是%d喔\n",t); system("pause"); return 0; } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.249.92.60 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1424050633.A.40E.html

02/16 09:43, , 1F
是*np,不是np
02/16 09:43, 1F

02/16 11:02, , 2F
同上,前者是指向位置裡的數值,後者是位置
02/16 11:02, 2F

02/17 05:02, , 3F
printf("指標np=n=%d\n",np) ; 這邊的np改成*np
02/17 05:02, 3F

02/17 05:05, , 4F
如果分開宣告 你應該比較懂這個意思
02/17 05:05, 4F

02/17 05:05, , 5F
int *np; np = &n;np是指向「位址」(address)
02/17 05:05, 5F
文章代碼(AID): #1KuKd9GE (C_and_CPP)