看板 C_and_CPP 關於我們 聯絡資訊
開發平台(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
Caesar08: 是*np,不是np 02/16 09:43
bben900911: 同上,前者是指向位置裡的數值,後者是位置 02/16 11:02
poolongkong: printf("指標np=n=%d\n",np) ; 這邊的np改成*np 02/17 05:02
poolongkong: 如果分開宣告 你應該比較懂這個意思 02/17 05:05
poolongkong: int *np; np = &n;np是指向「位址」(address) 02/17 05:05