作者zzhiphop6 (老頭)
看板C_and_CPP
標題[問題] 指標的練習問題
時間Mon Feb 16 09:37:10 2015
開發平台(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