Initialization array of two dimensional arrays

user2818508

How to initialize array of two dimensional arrays in C++ (defined like in code below)?

#include <iostream>
#include <array>

typedef int arr3by6Int[3][6];
typedef arr3by6Int arr3xarr3by6Int[3];

void print3by6(arr3by6Int arr)
{
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 6; j++)
        {
            std::cout << arr[i][j] << " ";
        }
        std::cout << std::endl;
    }
}
int main(int argc, char const *argv[])
{

    arr3by6Int a = {
        {1,2,3,4,5,6},
        {0,0,0,0,0,0},
        {2,2,2,2,2,2}
    };

    arr3by6Int b = {
        {2,2,3,4,5,6},
        {0,0,0,0,0,0},
        {2,2,2,2,2,2}
    };

    arr3by6Int c = {
        {3,2,3,4,5,6},
        {0,0,0,0,0,0},
        {2,2,2,2,2,2}
    };

    arr3xarr3by6Int d = { a, b, c };

    for(int i = 0; i < 3; i++)
    {
        print3by6(d[i]);
    }
    return 0;
}

I get these errors:

$ g++ -std=c++11 arrays.cpp -o arrays
arrays.cpp: In function ‘int main(int, const char**)’:
arrays.cpp:39:32: error: array must be initialized with a brace-enclosed initializer
arrays.cpp:39:32: error: array must be initialized with a brace-enclosed initializer
arrays.cpp:39:32: error: array must be initialized with a brace-enclosed initializer

jxh

You have #include <array> in your code, so you should use it. Change your types to use std::array<>:

typedef std::array<std::array<int, 6>, 3> arr3by6Int;
typedef std::array<arr3by6Int, 3> arr3xarr3by6Int;

Then, update your initialization lists to match:

    arr3by6Int a = {
        std::array<int, 6>{1,2,3,4,5,6},
        std::array<int, 6>{0,0,0,0,0,0},
        std::array<int, 6>{2,2,2,2,2,2}
    };

    arr3by6Int b = {
        std::array<int, 6>{2,2,3,4,5,6},
        std::array<int, 6>{0,0,0,0,0,0},
        std::array<int, 6>{2,2,2,2,2,2}
    };

    arr3by6Int c = {
        std::array<int, 6>{3,2,3,4,5,6},
        std::array<int, 6>{0,0,0,0,0,0},
        std::array<int, 6>{2,2,2,2,2,2}
    };

In most cases, an object of "C style" array type will degrade to the pointer to the array's first element when used in an expression. Your way of initializing d is attempting to initialize the 3 matrices with pointer values, which won't work.

A std::array is a class, so it won't degrade in that way.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Two-dimensional array initialization values

From Dev

Two-dimensional generic array initialization

From Dev

How to create a two dimensional array of two dimensional arrays?

From Dev

Ruby array of two dimensional arrays, search/lookup?

From Dev

How to create two dimensional array with 2 arrays

From Dev

Ruby array of two dimensional arrays, search/lookup?

From Dev

Get the number of arrays in a two dimensional array

From Dev

How to convert List of arrays to two dimensional array?

From Dev

Getting one-dimensional arrays from a two-dimensional array

From Dev

Two dimensional array from single dimensional arrays not working

From Dev

Create a 'results' array based on comparison of two two-dimensional arrays?

From Dev

How to fill an array of arrays (two-dimensional array)

From Dev

JNA two dimensional arrays

From Dev

In java how do I put two one-dimensional arrays into one two-dimensional array?

From Dev

Convert two one dimensional arrays to one two dimensional array- Matlab

From Dev

How to put arrays from Roo excel sheet in two dimensional array

From Dev

Sum of individual arrays inside two-dimensional array

From Dev

Creating an array of two-dimensional arrays in C#

From Dev

Merge two one dimensional String arrays to a single array with delimiter

From Dev

Laravel eloquent get one array of two dimensional arrays

From Dev

Two dimensional C# Array initialized with 2 other arrays

From Dev

C two dimensional arrays: Is the first 'level' an array of pointers?

From Dev

How to put arrays from Roo excel sheet in two dimensional array

From Dev

Two dimensional array - Make the child arrays keys same as all

From Dev

Sum of individual arrays inside two-dimensional array

From Dev

how to create multi-dimensional array from two or more arrays

From Dev

Multi-dimensional array initialization

From Dev

Computing two dimensional arrays in memory

From Dev

How to sum two dimensional arrays

Related Related

  1. 1

    Two-dimensional array initialization values

  2. 2

    Two-dimensional generic array initialization

  3. 3

    How to create a two dimensional array of two dimensional arrays?

  4. 4

    Ruby array of two dimensional arrays, search/lookup?

  5. 5

    How to create two dimensional array with 2 arrays

  6. 6

    Ruby array of two dimensional arrays, search/lookup?

  7. 7

    Get the number of arrays in a two dimensional array

  8. 8

    How to convert List of arrays to two dimensional array?

  9. 9

    Getting one-dimensional arrays from a two-dimensional array

  10. 10

    Two dimensional array from single dimensional arrays not working

  11. 11

    Create a 'results' array based on comparison of two two-dimensional arrays?

  12. 12

    How to fill an array of arrays (two-dimensional array)

  13. 13

    JNA two dimensional arrays

  14. 14

    In java how do I put two one-dimensional arrays into one two-dimensional array?

  15. 15

    Convert two one dimensional arrays to one two dimensional array- Matlab

  16. 16

    How to put arrays from Roo excel sheet in two dimensional array

  17. 17

    Sum of individual arrays inside two-dimensional array

  18. 18

    Creating an array of two-dimensional arrays in C#

  19. 19

    Merge two one dimensional String arrays to a single array with delimiter

  20. 20

    Laravel eloquent get one array of two dimensional arrays

  21. 21

    Two dimensional C# Array initialized with 2 other arrays

  22. 22

    C two dimensional arrays: Is the first 'level' an array of pointers?

  23. 23

    How to put arrays from Roo excel sheet in two dimensional array

  24. 24

    Two dimensional array - Make the child arrays keys same as all

  25. 25

    Sum of individual arrays inside two-dimensional array

  26. 26

    how to create multi-dimensional array from two or more arrays

  27. 27

    Multi-dimensional array initialization

  28. 28

    Computing two dimensional arrays in memory

  29. 29

    How to sum two dimensional arrays

HotTag

Archive