将数组名作为变元传递给“被调用函数“

 #include <stdio.h>
 
 void change_array(char*, int);
 
 int main(void)
 {
     char a[10] = "abcdefg";
     printf("The address of origin a = %p\n",a);
     change_array(a, 10);
     printf("The address of later a = %p\n",a);
     printf("%s\n",a);
     return 0;
 }
 
 void change_array(char *a, int n)
 {
     scanf("%s", a);//To my surprise, this gets the same result as using either a or &a.
     //a = "AAAAA";//a在这实质是指针,因为数组名本来就是一个地址,经过函数调用制作了副本后,
     //a,着实变成了一个指针。in other words,when 'a' refer to "AAAAA",it turn 'a' refer to 
     //a new address.but the origin 'a' is a array not a piont,it failed to change the value of array 'a'.
 }
 //在main函数中,a是一个数组。当用数组名作为变元,传递给change_array函数时,并不是将原来数组名的地址传递过去。
 //而是,制作一个数组名地址的副本,通过这个副本依然可以访问数组。在change_array函数里,传递过来的副本被当成一个
 //指针来处理,所以可以将指针重新指向一个地址,如a = "AAAAA",但不要忘记,在main函数中,a是一个数组,不能修改数组
 //的地址。所以无论在change_array中的指针a指任何地址,在main中的数组a的地址是不变的。——————“可以修改指针包含的地址,
 //不能改变数组名称引用的地址。-->数组与指针的基本区别" 用scanf函数时,实质上是将数组a的地址里的值“踢走”,再将在
 //输入流(键盘)取得的值放进数组a里。

 

posted @ 2016-07-23 11:03  busui  阅读(652)  评论(0编辑  收藏  举报