Home > Backend Development > C++ > Initializing multidimensional arrays in C/C++

Initializing multidimensional arrays in C/C++

王林
Release: 2023-09-15 15:29:01
forward
692 people have browsed it

In multi-dimensional arrays, the dimension of the array should be greater than 1. The figure below shows the memory allocation strategy for a multi-dimensional array with dimensions of 3 x 3 x 3.

Initializing multidimensional arrays in C/C++

This is a program written in C to initialize a multi-dimensional array.

Algorithm

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

Example

#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;
}
Copy after login

Output

Size of the Array:16
Content of the Array:16
3 1
7 6
Copy after login

The above is the detailed content of Initializing multidimensional arrays in C/C++. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template