Maison  >  Article  >  développement back-end  >  Initialisation de tableaux multidimensionnels en C/C++

Initialisation de tableaux multidimensionnels en C/C++

王林
王林avant
2023-09-15 15:29:01640parcourir

Dans les tableaux multidimensionnels, la dimension du tableau doit être supérieure à 1. La figure suivante montre la stratégie d'allocation de mémoire pour un tableau multidimensionnel de dimensions 3 x 3 x 3.

Initialisation de tableaux multidimensionnels en C/C++

Il s'agit d'un programme écrit en C++ pour initialiser un tableau multidimensionnel.

Algorithme

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

Exemple

#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;
}

Sortie

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

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Cet article est reproduit dans:. en cas de violation, veuillez contacter admin@php.cn Supprimer