zoukankan      html  css  js  c++  java
  • 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]#



  • 相关阅读:
    软件测试常见概念
    Apollo简介及工作原理
    bug的编写技巧与级别划分
    native与H5优缺点及H5测试
    优惠券测试
    go语言-for循环
    go语言-流程控制--if
    go语言-二进制与位运算
    cookie和session
    AJAX
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6952426.html
Copyright © 2011-2022 走看看