how to initialize a 2d array in c code example

Example 1: how to declare multidimensional array in c

int a[3][4] = {  
   {0, 1, 2, 3} ,   /*  initializers for row indexed by 0 */
   {4, 5, 6, 7} ,   /*  initializers for row indexed by 1 */
   {8, 9, 10, 11}   /*  initializers for row indexed by 2 */
};

Example 2: matrix c declaration

int disp[2][4] = { 10, 11, 12, 13, 14, 15, 16, 17};

Example 3: double array in c

int disp[2][4] = {
    {10, 11, 12, 13},
    {14, 15, 16, 17}
};

Example 4: initialize 2d array c++

#include <array>
2 #include <iostream>
3
4 using namespace std;
5
6 //remember const!
7 const int ROWS = 2;
8 const int COLS = 3;


int main(){
 array<array<int, COLS>, ROWS> matrix = {
 1, 2, 3,
 4, 5, 6
 };
  
  
  return 0;
}

Tags:

Java Example