<div align="left"><pre class="cpp" name="code">上午上《編譯原理》的時候聽老師談到了C語言的指針的問題,有些疑問,所以回到宿捨進行了實驗驗證。
<p>當函數參數是個指針的時候,當調用這個函數時,此函數的形參的得到的是指針本身,還是他的一個副本?如下代碼:</p><p>#include "stdio.h"
void change1(int* d)
{
printf("address_d = %d d = %d *d = %d\n", &d, d, *d);
int s = 4;
d = &s;
printf("address_d = %d d = %d *d = %d\n",&d, d, *d);
}
void change2(int *e)
{
*e = 5;
}
int main()
{
int *p(NULL);
int *q(NULL);
int a(1);
while(a)
{
scanf("%d", &a);
printf("address_a = %d a = %d\n", &a, a);
p = &a;
q = p;
printf("address_P = %d p = %d *p = %d\n", &p, p, *p);
printf("address_q = %d q = %d *q = %d\n", &q, q, *q);
change1(q);
printf("address_q = %d q = %d *q = %d\n", &q, q, *q);
change2(q);
printf("address_a = %d a = %d\n", &a, a);
printf("address_q = %d q = %d *q = %d\n", &q, q, *q);
printf("address_P = %d p = %d *p = %d\n", &p, p, *p);
}
}
</p>
作者 wche1990