zoukankan      html  css  js  c++  java
  • C++ 数组、二维数组、函数参数

    一维数组:

     1 #include <iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     /**为了方便 数组的类型全部都是int类型的*/
     6     int numbers[5] = {1,2,3,4,5};
     7     /**通过指针的方式访问数组*/
     8     int * p = numbers;
     9     cout << "普通指针的方式访问:
    ";
    10     for (int i = 0; i < 5; i++)
    11         cout << p[i] << " ";
    12     cout << endl;
    13     /**
    14     在这里面numbers 被解释为数组的第一个元素的地址 即 &numbers[0] 而且还是一个常量
    15     也有特殊的情况,例如:sizeof numbers是数组大小
    16     */
    17     cout << "数组大小:";
    18     cout << sizeof numbers << " Byte.
    ";// 5 * 4
    19     /**通过数组指针的方式访问数组
    20     数组的地址是 &numbers NOTE:这和numbers值是一样的!!!
    21     也就是说相同的地址 你可以用普通的指针指向 也可以用数组指针指向
    22     区别就在于他们的指针运算
    23     */
    24     int (*p2)[5] = &numbers;//把数组的numbers替换成(*p2)就是 数组的指针了
    25     cout << "指针数组的方式访问:
    ";
    26     for (int i = 0; i < 5; i++)
    27         cout << (*p2)[i] << " ";
    28     cout << endl;
    29     /**
    30     int * p 和 int (*p2)[5] 的区别
    31     区别在于他们的指针运算
    32     这里不明白的去百度 指针运算
    33     */
    34     cout << "p = " << p << endl;
    35     cout << "p + 1 = " << p + 1 << endl;//4Byte
    36     cout << "p2 = " << p2 << endl;
    37     cout << "p2 + 1 = " << p2 + 1 << endl;//20 Byte
    38     /**提到 数组指针 就必去提到另一个知识点 指针数组
    39     数组指针 是 指针
    40     指针数组 是 数组
    41     定义的时候 二者通常会混淆
    42     */
    43     int * p3[5];//指针数组 每一个元素 都是指针
    44     for (int i = 0; i < 5; i++)
    45         p3[i] = (*p2) + i;//p2是数组指针 常作为二维数组的指针使用
    46     cout << "我也不知道这是什么操作:
    ";
    47     for (int i = 0; i < 5; i++)
    48         cout << *p3[i] << " ";
    49     cout << endl;
    50     return 0;
    51 }

    二维数组:

     1 #include <iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     int data[3][5] =
     6     {
     7         {1,2,3,4,5},
     8         {9,8,7,6,5},
     9         {1,9,9,5,1}
    10     };
    11     /**二维数组的本质是 数组的数组、
    12     第一维数组是3个一维数组的地址、第二维就是普通的数组*/
    13     int * p[3] = {data[0], data[1], data[2]};
    14     cout << "地址:
    ";
    15     for (int i = 0; i < 3; i++)
    16         cout << p[i] << "   ";
    17     cout << endl;
    18     /**常规方式访问二维数组*/
    19     cout << "常规方式访问二维数组
    ";
    20     for (int i = 0; i < 3; i++)
    21     {
    22         for (int j = 0; j <5; j++)
    23             cout << data[i][j] << " ";
    24         cout << endl;
    25     }cout << endl;
    26     /**用指针数组尝试访问一下*/
    27     cout << "用指针数组皮一下
    ";
    28     for (int i = 0; i < 3; i++)
    29     {
    30         for (int j = 0; j < 5; j++)
    31             cout << p[i][j] << " ";//这里之所以能访问是因为size是int
    32         cout << endl;
    33     }cout << endl;
    34     /**用数组指针
    35     int data[3][5] 的第一个元素就是data[0]那么他的 地址就是&data[0] 等价于data
    36     前面说过data是属于的第一个元素的地址, 而且还是一个常量
    37     那么问题来了 指向&data[0]的指针是什么样子的?
    38     data[0] 的类型是int[5] 那么
    39     &data[0] 的类型就是 int (*)[5]
    40     */
    41     int (*p2)[5] = data;
    42     cout << "用数组指针访问
    ";
    43     for (int i = 0 ; i < 3; i++)
    44     {
    45         for (int j = 0; j < 5; j++)
    46             cout << p2[i][j] << " ";//这里参考一维数组的数组指针就能理解了
    47         cout << endl;
    48     }cout << endl;
    49     return 0;
    50 }

     数组与函数:

     1 #include <iostream>
     2 using namespace std;
     3 void print1(const int * p, int len);
     4 void print2(const int p[], int len);
     5 void print3(int p[][5], int len);//don't use const  DON'T!!!
     6 void print4(int (*p)[5], int len);//don't use const  DON'T!!!
     7 int main()
     8 {
     9     int numbers[5] = {1,2,3,4,5};
    10     int data[3][5] =
    11     {
    12         {1,2,3,4,5},
    13         {5,6,7,8,9},
    14         {9,8,7,6,5}
    15     };
    16     print1(numbers, 5);
    17     print2(numbers, 5);
    18     print3(data, 3);
    19     print4(data, 3);
    20     return 0;
    21 }
    22 void print1(const int* p, int len)
    23 {
    24     cout << "print1
    ";
    25     for (int i = 0; i < len; i++)
    26         cout << p[i] << " ";
    27     cout << endl;
    28 }
    29 void print2(const int p[], int len)
    30 {
    31     cout << "print2
    ";
    32     for (int i = 0; i < len; i++)
    33         cout << *(p + i) << ' ';
    34     cout << endl;
    35 }
    36 void print3(int p[][5], int len)
    37 {
    38     cout << "print3
    ";
    39     for (int i = 0; i < len; i++)
    40     {
    41         for (int j = 0; j < 5; j++)
    42             cout << p[i][j] << ' ';
    43         cout << endl;
    44     }cout << endl;
    45 }
    46 void print4(int(* p)[5], int len)
    47 {
    48     cout << "print4
    ";
    49     for (int i = 0; i < len; i++)
    50     {
    51         for (int j = 0; j < 5; j++)
    52             cout << p[i][j] << ' ';
    53         cout << endl;
    54     }cout << endl;
    55 }
    56 /**
    57 []([0]) 和 * 是相同的
    58 * 就是[0]
    59 p[5] 可以写成 * (p + 5)
    60 p[5]实际做的事情就是先寻址然后在取值的过程
    61 谭浩强老师在他的书里面讲过
    62 */

    补充:

     1 #include <iostream>
     2 using namespace std;
     3 int main()
     4 {
     5     /**
     6     const int * p 和 int * const p的区别
     7     */
     8     int a = 10;
     9     const int b = 20;
    10     const int * p1;// *p1 is read-only
    11     p1 = &a;
    12     cout << "p1 = " << p1 << endl;
    13     cout << "&a = " << &a << endl;
    14     cout << "*p1 = " << *p1 << endl;
    15     //ERROR   *p1 = 23;
    16     p1 = &b;// p1 is not read-only
    17     cout << "p1 = &b
    ";
    18     cout << "*p1 = " << *p1 << endl << endl;
    19 
    20     int * const p2 = &a;// p2 is read-only and must init it
    21     //int * const p2 = &b;不匹配 因为没有保护数据
    22     //const int * const p2 = &b; 这样可以
    23     cout << "p2 = " << p2 << endl;
    24     cout << "*p2 = " << *p2 << endl;
    25     int c = 1222;
    26 //    p2 = &c; erro p2 is read-only
    27     *p2 = 1234567;
    28     cout << "*p2 = " << *p2 << endl;
    29     cout << "p2 = " << p2 << endl;
    30     return 0;
    31 }

  • 相关阅读:
    微信登录
    Nginx负载均衡的优缺点
    elk 比较不错的博客
    Filebeat 5.x 日志收集器 安装和配置
    日志管理系统ELK6.2.3
    python3爬虫编码问题
    zabbix监控进程
    linux下查询进程占用的内存方法总结
    Ubuntu 16.04安装Elasticsearch,Logstash和Kibana(ELK)Filebeat
    ELK多种架构及优劣
  • 原文地址:https://www.cnblogs.com/zuosy/p/8580656.html
Copyright © 2011-2022 走看看