首頁  >  文章  >  後端開發  >  在C/C++中初始化多維數組

在C/C++中初始化多維數組

王林
王林轉載
2023-09-15 15:29:01640瀏覽

在多維數組中,數組的維數應該大於1。下圖展示了一個維數為3 x 3 x 3的多維數組的記憶體分配策略。

在C/C++中初始化多維數組

這是一個用C 寫的初始化多維數組的程式。

演算法

Begin
   Initialize the elements of a multidimensional array.
   Print the size of the array.
   Display the content of the array.
End

範例

#include<iostream>
using namespace std;
int main()
{
   int r, c;
   int a[][2] = {{3,1},{7,6}};
   cout<< "Size of the Array:"<<sizeof(a)<<"\n";
   cout<< "Content of the Array:"<<sizeof(a)<<"\n";
   for(r=0; r<2; r++) {
      for(c=0; c<2; c++) {
         cout << " " << a[r][c];
      }
      cout << "\n";
   }
   return 0;
}

輸出

Size of the Array:16
Content of the Array:16
3 1
7 6

以上是在C/C++中初始化多維數組的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除