#include<iostream>
using namespace std;
//打印二维数组
void print(int arr1[2][3])
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout << arr1[i][j] << " ";
}
cout << endl;
}
}
int main()
{
//二维数组定义方式
//1.数据类型 数组名[行数][列数]
int arr1[2][3];
arr1[0][0] = 1;
arr1[0][1] = 2;
arr1[0][2] = 3;
arr1[1][0] = 4;
arr1[1][1] = 5;
arr1[1][2] = 6;
//print(arr1);
//cout << arr1[0][0] << endl;
//2.数据类型 数组名[行数][列数]={{数据1,数据2},{数据3,数据4}};
int arr2[2][3] =
{
{0,1,2},
{3,4,5}
};
//print(arr2);
//3.数据类型 数组名[行数][列数]={数据1,数据2,数据3,数据4}
int arr3[2][3] = { 1,2,3,4,5,6 };
//print(arr3);
//4.数据类型 数组名[][列数]={数据1,数据2,数据3,数据4}
int arr4[][3] = { 1,2,3,4,5,6 };
print(arr4);
cout <<"一共有"<< sizeof(arr4) / sizeof(arr4[0][0])<<"个元素" << endl;//元素个数
cout <<"数组首地址"<<(int)arr4 << endl;//数组首地址
int num = 1;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 3; j++)
{
cout <<"第"<<num<< "个元素地址:" << endl;
cout <<(int)&arr4[i][j] << endl;//每个元素地址
num++;
}
}
cout << "第一行占用内存"<<sizeof(arr4[0]) << endl;
cout << "二维数组行数:" << sizeof(arr4) / sizeof(arr4[0]) << endl;
cout << "二维数组列数:" << sizeof(arr4[0]) / sizeof(arr4[0][0]) << endl;
cout << "第一行首地址:" << (int)arr4[0] << endl;//与第一个元素和数组首地址重合
cout << "第二首地址:" << (int)arr4[1] << endl;
system("pause");
return 0;
}
网友评论