C专家编程之为什么C语言把数组形參当做指针:数组/指针实參

#include<stdio.h>
void print_array_test(char ca[])
{
        printf("ca : %s
",ca);
        printf("&ca : %s
",&ca);
        printf("&(ca[0]) : %s
",&(ca[0]));
        printf("&(ca[1]) : %s
",&(ca[1]));
        printf("*(ca+0) : %c
",*(ca+0));
        printf("*(ca+1) : %c
",*(ca+1));
        printf("ca+1 : %s
",ca+1);
}


void print_ptr_test(char *pa)
{
        printf("pa : %s
",pa);
        printf("&pa : %s
",&pa);
        printf("&(pa[0]) : %s
",&(pa[0]));
        printf("&(pa[1]) : %s
",&(pa[1]));
        printf("pa+1 : %s
",pa+1);
        printf("pa[1] : %c
",pa[1]);
        printf("*(pa+1) : %c
",*(pa+1));
        printf("++pa : %s
",++pa);
}
int main()
{
        char abc[]="hello,world!";
        print_array_test(abc);
        print_ptr_test(abc);

        return 0;
}

数组abc[20]="hello,world!"

中&abc[i]与abc+i含义同样——取的是abc[i]的地址

指针pa =abc;

pa[i]与*(pa+i)是等价的——取的是abc[i]的值

鉴于以上程序,执行之:

[root@localhost code]# ./arrayandptr
ca : hello,world!
&ca : ▒▒▒,▒▒;
&(ca[0]) : hello,world!
&(ca[1]) : ello,world!
*(ca+0) : h
*(ca+1) : e
ca+1 : ello,world!
pa : hello,world!
&pa : ▒▒▒,▒▒;
&(pa[0]) : hello,world!
&(pa[1]) : ello,world!
pa+1 : ello,world!
pa[1] : e
*(pa+1) : e
++pa : ello,world!
[root@localhost code]#



原文地址:https://www.cnblogs.com/cynchanpin/p/6952426.html