[C Tips] 数组名一定代表指向数组第一个元素的指针--------吗?
无数的教科书都几乎没有严肃认真的面对这个问题。数组名就一定代表指向数组第一个元素的指针吗?猜猜下面的代码输出是怎么样的:
char ga[] = "abcdefghijklm";
void my_array_func(char ca[10]){
printf("address of array param = %#x\n",&ca);
printf("address of ca[0] = %#x\n",&ca[0]);
printf("address of ca[1]= %#x\n",&ca[1]);
printf("++ca = %#x\n\n",++ca);
}
void my_pointer_func(char *pa){
printf("address of ptr param = %#x\n",&pa);
printf("address of pa[0] = %#x\n",&pa[0]);
printf("address of pa[1]= %#x\n",&pa[1]);
printf("++pa = %#x\n\n",++pa);
}
int main()
{
printf("address of global array = %#x \n",&ga);
printf("address of ga[0] = %#x \n",&ga[0]);
printf("address of ga[1] = %#x \n\n",&ga[1]);
my_array_func(ga);
my_pointer_func(ga);
system("PAUSE");
return 0;
}
如果你发现结果数组参数和数组参数第一个元素的地址不一样,不要太吃惊,请在两个my_func里面打印看看*(&ca)和*(&pa)的内容看看。如果你在此时用sizeof看看参数数组的大小你会更吃惊的,自己试试吧;-)void my_array_func(char ca[10]){
printf("address of array param = %#x\n",&ca);
printf("address of ca[0] = %#x\n",&ca[0]);
printf("address of ca[1]= %#x\n",&ca[1]);
printf("++ca = %#x\n\n",++ca);
}
void my_pointer_func(char *pa){
printf("address of ptr param = %#x\n",&pa);
printf("address of pa[0] = %#x\n",&pa[0]);
printf("address of pa[1]= %#x\n",&pa[1]);
printf("++pa = %#x\n\n",++pa);
}
int main()
{
printf("address of global array = %#x \n",&ga);
printf("address of ga[0] = %#x \n",&ga[0]);
printf("address of ga[1] = %#x \n\n",&ga[1]);
my_array_func(ga);
my_pointer_func(ga);
system("PAUSE");
return 0;
}