數組-二維數組的定義方式、二維數組名用途


  • 外層循環打印行數,內層循環打印列數
點擊查看代碼
#include<iostream>
#include<string> 

using namespace std;


int main()
{
	//2. 數據類型數組名[ 行數 ][ 列數 ] = { {數據1,數據2 },{數據3,數據4 } };
	int arr[2][3] = 
	{
		{1,2,3},
		{4,5,6}
	};

	for(int i = 0; i < 2; i++)
	{
		for(int j = 0; j < 3; j++)
		{
			cout << arr[i][j] << " ";
		}
		
		cout << endl;
	}

	//4.數據類型數組名[ ][ 列數 ] = { 數據1,數據2,數據3,數據4 };
	int arr01[][3] = { 11,21,31,41,51,61 };
	for(int i = 0; i < 2; i++)
	{
		for(int j = 0; j < 3; j++)
		{
			cout << arr01[i][j] << " ";
		}
		
		cout << endl;
	}

	system("pause");

	return 0;
}

 

點擊查看代碼
int arr[2][3] = 
	{
		{1,2,3},
		{4,5,6}
	};

	cout << "二維數組所占內存空間:" << sizeof(arr) << endl;
	cout << "二維數組第一行占內存空間:" << sizeof(arr[0]) << endl;
	cout << "二維數組第一個元素占內存空間:" << sizeof(arr[0][0]) << endl;

點擊查看代碼
	cout << "二維數組首地址:" << (int)arr << endl;
	cout << "二維數組第一行首地址:" << (int)arr[0] << endl;
	cout << "二維數組第一個元素首地址:" << (int)&arr[0][0] << endl;
	cout << "二維數組第二行首地址:" << (int)arr[1] << endl;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM