ホームページ  >  記事  >  バックエンド開発  >  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 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事はtutorialspoint.comで複製されています。侵害がある場合は、admin@php.cn までご連絡ください。