C++数组和指针

数组必须在定义时初始化。

数组名不能被赋值。

数组名可以作为地址赋给指针。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     int b[10];
 8     //b[10] = {1,2,3,4};//错误
 9     int c[] = {2,3,1,2};
10     int *a;
11     a = b;
12     cout << a[0] << endl;
13     //c = b;//错误
14     system("pause");
15     return 0;
16 }

 当对数组名使用sizeof时,返回数组的长度。

当对取地址的数组名使用sizeof时,返回4个字节的地址类型长度。

数组名加1后,地址增加基本类型的长度。

数组名取地址加1后,地址增加数组的长度。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     short c[] = {2,3,1,2};
 8     cout << hex;
 9     cout << &c[0] << endl;
10     cout << &c << endl;
11     cout << sizeof c << endl;
12     cout << sizeof &c << endl;
13     cout << c+1 << endl;
14     cout << &c+1 << endl;
15 
16     system("pause");
17     return 0;
18 }
原文地址:https://www.cnblogs.com/sungnox/p/7586993.html