作者su35 (su35)
看板C_and_CPP
標題[問題] 請問關於指標問題
時間Thu Jul 2 00:17:45 2009
下面是我剛剛在自己學指標 發現的問題
#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
1F:推 Bencrie:問題一 ... 我註解拿掉編譯會過 07/03 10:21
2F:→ Bencrie:問題二 ... y在離開test1以後就已經無效了,要嘛用malloc 07/03 10:23
3F:→ Bencrie:不然就加 static。 07/03 10:23
4F:→ su35:感謝 07/05 23:11