zoukankan      html  css  js  c++  java
  • 第35课 数组参数和指针参数分析

    为什么C语言中的数组参数会退化为指针?

    退化的意义:

    二维数组参数:

    等价关系:

    知识点:

    传递与访问二维数组示例:

     1 #include <stdio.h>
     2 
     3 void access(int a[][3], int row)
     4 {
     5     int col = sizeof(*a) / sizeof(int);
     6     int i = 0;
     7     int j = 0;
     8     
     9     printf("sizeof(a) = %d
    ", sizeof(a));
    10     printf("sizeof(*a) = %d
    ", sizeof(*a));
    11     
    12     for(i=0; i<row; i++)
    13     {
    14         for(j=0; j<col; j++)
    15         {
    16             printf("%d
    ", a[i][j]);
    17         }
    18     }
    19     
    20     printf("
    ");
    21 }
    22 
    23 void access_ex(int b[][2][3], int n)
    24 {
    25     int i = 0;
    26     int j = 0;
    27     int k = 0;
    28     
    29     printf("sizeof(b) = %d
    ", sizeof(b));
    30     printf("sizeof(*b) = %d
    ", sizeof(*b));
    31     
    32     for(i=0; i<n; i++)
    33     {
    34         for(j=0; j<2; j++)
    35         {
    36             for(k=0; k<3; k++)
    37             {
    38                 printf("%d
    ", b[i][j][k]);
    39             }
    40         }
    41     }
    42     
    43     printf("
    ");
    44 }
    45 
    46 int main()
    47 {
    48     int a[3][3] = {{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
    49     int aa[2][2] = {0};
    50     int b[1][2][3] = {0};
    51     
    52     access(a, 3);
    53     access(aa, 2);
    54     access_ex(b, 1);
    55     access_ex(aa, 2);
    56     
    57     return 0;
    58 }

    53行传递的参数与形参类型不匹配,访问会出现意想不到的结果。55行也是类型不匹配。

    注释掉53、55行,运行结果如下:

    小结:

  • 相关阅读:
    剑指offer51-正则表达式匹配
    剑指offer50-构建乘积数组
    剑指offer49-数组中的重复
    PHP系列笔记——Zend_Controller工作流程
    多态与重载
    读取文件数据的大概流程
    基于HTTP协议下载文件的实现
    C++中的面向对象笔记
    firebreath注册接口
    python读取excelxlsx,写入excel
  • 原文地址:https://www.cnblogs.com/wanmeishenghuo/p/9545628.html
Copyright © 2011-2022 走看看