1. 程式人生 > >C++ 數組、二維數組、函數參數

C++ 數組、二維數組、函數參數

uri esp 操作 屬於 另一個 普通 第一個 做的 turn

一維數組:

 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 << "普通指針的方式訪問:\n";
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.\n";// 5 * 4 19 /**通過數組指針的方式訪問數組 20 數組的地址是 &numbers NOTE:這和numbers值是一樣的!!!
21 也就是說相同的地址 你可以用普通的指針指向 也可以用數組指針指向 22 區別就在於他們的指針運算 23 */ 24 int (*p2)[5] = &numbers;//把數組的numbers替換成(*p2)就是 數組的指針了 25 cout << "指針數組的方式訪問:\n"; 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 << "我也不知道這是什麽操作:\n"; 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 << "地址:\n";
15     for (int i = 0; i < 3; i++)
16         cout << p[i] << "   ";
17     cout << endl;
18     /**常規方式訪問二維數組*/
19     cout << "常規方式訪問二維數組\n";
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 << "用指針數組皮一下\n";
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 << "用數組指針訪問\n";
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\n";
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\n";
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\n";
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\n";
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\n";
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 }

技術分享圖片

C++ 數組、二維數組、函數參數