一維數組名的作用:
1.可以統計整個數組在內存中的長度。
2.可以獲取數組在內存中的首地址。
3.數組名是常量,不可以進行賦值操作。
#include <iostream>
using namespace std;
int main()
{
/*
1.int佔4個字節(32位機器)
2.一個字節等於八位
*/
int arr[5] = { 1,2,3,4,5};
cout << "整個數組佔用內存空間爲:" << sizeof(arr) << endl;//20
cout << "每個元素佔用內存空間爲:"<<sizeof(arr[0]) << endl;//4
cout << "數組中元素個數爲:" << sizeof(arr) / sizeof(arr[0]) << endl;//5
/*
1.可以通過數組名查看數組首地址
2.(int)&arr[0]:將16進制地址轉爲10進制
3.數組首地址與數組中的第一個元素地址相同
4.數組中相鄰元素之間的地址相差一個數據類型佔用的字節大小
*/
cout << "數組首地址:" << (int)arr << endl;//20183080
cout << "數組中第一個元素地址爲:" << (int)&arr[0] << endl;//20183080
cout << "數組中第二個元素地址爲:" << (int)&arr[1] << endl;//20183084
system("pause");
return 0;
}