[問題] Pointer宣告、call function
最近在自學C/C++ 在Pointer遇到不少問題
想來請教各位:
1. 下例是C(m,n)的code
#include <stdio.h>
int CMN(int *, int);
int main() {
int i, j, *a;
a=&i;
printf("Input the base and times:");
scanf("%d %d", &i, &j);
printf("\nThe answer is %d", CMN(*a,j));
}
/* 上面會出現錯誤,但如果把星號去除,就沒問題
想問的是,會出現錯誤是因為 a=&i,所以 *a=i的值
而在CMN的定義 CMN(int *, int)已經說第1個一定是傳address
如果寫成 CMN(*a, j) 就違反定義了,因為 *a傳的是value
這樣理解是對的嗎? 還是有其他原因呢?
*/
int CMN (int *p, int q) {
int sum=1;
if (q == 0)
return 1;
else
for (int c=1;c<=q;c++)
{
sum = sum * *p;
}
return sum;
}
2.
int a=8, *p=&a;
跟下面這樣寫一樣嗎?
int a=8, *p;
p=&a;
3.
int a=8, *p=&a, &b=*p;
b沒有宣告過,這樣合理嗎? b=8?
我看教學影片是說 &b不是b的位址,只是一個reference
但我不太懂b是要refernce什麼? 他在記憶體裡會有空間嗎?
謝謝各位的回應!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.167.161.46
→
07/17 19:30, , 1F
07/17 19:30, 1F
→
07/17 19:30, , 2F
07/17 19:30, 2F
→
07/17 19:32, , 3F
07/17 19:32, 3F
→
07/17 19:32, , 4F
07/17 19:32, 4F
→
07/17 19:33, , 5F
07/17 19:33, 5F
→
07/17 23:25, , 6F
07/17 23:25, 6F
→
07/17 23:26, , 7F
07/17 23:26, 7F
推
07/20 12:28, , 8F
07/20 12:28, 8F
→
07/20 12:28, , 9F
07/20 12:28, 9F