首页  >  文章  >  后端开发  >  在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删除