[問題] 請問關於指標問題

看板C_and_CPP (C/C++)作者 (su35)時間16年前 (2009/07/02 00:17), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串1/1
下面是我剛剛在自己學指標 發現的問題 #include <stdio.h> #include <stdlib.h> void main(void) { int *a; int b=5; a=&b; printf("%d\n",a); printf("%d\n",&b); printf("%d\n",&a); printf("%d\n",*a); int *c; int* test1(int* a); //********************** 問題一 請問我為什麼一定要寫成這樣 才能編譯過 我本來想寫的是 /* a=test1(a); printf("-----------------\n"); printf("%d\n",*a); */ a=test1(a); c=test1(a); printf("-----------------\n"); printf("%d\n",*c); //********************** system("pause"); } int* test1(int* a) { //********************* //問題二 我本來想寫這樣 但得到的答案看起來想記憶體位址 為什麼會這樣 /* int* x; int y=0; x=&y; *x=*a * *a; return x; */ //******************* *a=*a * *a; return a; } -- ╲ ◢▅◢▅◣ ╱ 親愛的妳要找什麼阿~██ ███在前面!!! ◤◥◥ ◢◢◢ ◥◥我找到了! 為什麼...我的心裡 你在這邊 ◣-- 會有種蛋蛋的哀傷呢? ◣▽ ◢ 等我一下唷~ ▂▂▂◣╯ ▂▂▂▂▂▂▂╯ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.116.105.76

07/03 10:21, , 1F
問題一 ... 我註解拿掉編譯會過
07/03 10:21, 1F

07/03 10:23, , 2F
問題二 ... y在離開test1以後就已經無效了,要嘛用malloc
07/03 10:23, 2F

07/03 10:23, , 3F
不然就加 static。
07/03 10:23, 3F

07/05 23:11, , 4F
感謝
07/05 23:11, 4F
文章代碼(AID): #1AIumjS1 (C_and_CPP)
文章代碼(AID): #1AIumjS1 (C_and_CPP)