1. 程式人生 > 其它 >陣列-二維陣列的定義方式、二維陣列名用途

陣列-二維陣列的定義方式、二維陣列名用途

  • 外層迴圈列印行數,內層迴圈列印列數
點選檢視程式碼
#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;