Initializing Array of Arrays - Trouble

23,915

Solution 1

I can see why this is useful, however, in C, using just the variable name of an array will return the address of the array in memory. The compiler has no idea as to what is actually stored at A during compile time, so this wouldn't work.

As an alternative, what you could do is either use a memcpy and copy the elements into the array (but then it won't be const), or you could use a #define A { 1, 2, 3, 4 } and then maybe do something like:

#define A_val { 1, 2, 3, 4 }
#define B_val { 5, 6, 7, 8 }
const U64 multiDimArray[12][64] = {
    A_val,
    B_val,
    // and so on and so forth
}
const U64 A[4] = A_val; // if you need this
const U64 B[4] = B_val; // you can do it like this

Solution 2

If you use C++11, the initializer list for an array is flexible:

std::array< array<U64, 64>, 12> multiDimArray = {
     A, B, C, D, E, F,  
     G, H, I, J, K, L
};

will work fine, assuming A..L are std::array<64, U64>.

The array does have no overhead to the c-style array. Click here for official reference.

"The size and efficiency of array for some number of elements is equivalent to size and efficiency of the corresponding C-style array T[N]." (From the reference)


I said "flexible", since you can use a mixed initializer-list like this:

std::array<int, 3> first_row = {1,2,3};
std::array<array<int, 3>, 2> a={
  first_row,
  {2, 2, 2}
};

You can use this as a fixed-size array, with the same operations:

a[1][2]=2; a[0][1]=1; a[0][2]=3;

Solution 3

You can use the fact that a two dimensional array is actually a one dimensional array of pointers to your advantage here. The follow initialization should work for you.

const U64 *multiDimArray[12] = { 
     A, B, C, D, E, F,  
     G, H, I, J, K, L
};
Share:
23,915
Dr.Kameleon
Author by

Dr.Kameleon

The Arturo Programming Language Who am I? A Greek-born Spain-based programmer and former Med-student, who has been in love with the Art of Code since he was a 7-year-old kid and would not change it for anything in this world. Current Position : Chief Software Architect &amp; Founder @ InSili.co Expertise C / D / Nim Flex / Bison Objective-C / Cocoa Swift Ruby LaTeX HTML / CSS JavaScript / Node.js PHP / SQL CodeIgniter / Wordpress Also playing with : Assembly C++ Haskell Perl Pascal / Delphi Prolog Programming Interests Low-level engineering: compilers, interpreters, programming languages, operating system kernels macOS/Cocoa development Chess engines Data extraction, scraping and automation Website design &amp; development Typesetting &amp; e-publishing Projects Open-Source: Arturo - Programming language &amp; Bytecode Stack-based VM interpreter -- in Nim/C Delicious - A library of Swift language extensions and Cocoa utilities for macOS/iOS -- in Swift Logramm - Programming language &amp; Interpreter -- in D MathMachine - A Math-Oriented programming language &amp; Interpreter -- in C++ Bogart - A chess engine project -- in C++ FoldWatcher - A (rather ancient) Folding@Home GUI Client -- in C++/Qt Nemesis - A (rather ancient too) Remote Desktop Client-Server app -- in Pascal/Delphi &amp; various more... Commercial: iSwift - An Objective-C to Swift converter &amp; various Swift-related resources Peppermint - Code editor for macOS Squeezer - Resource-compression app for HTML/CSS/JS/images for macOS JSON Wizard - JSON viewer &amp; editor for macOS &amp; various more... Books: Swift 4 recipes - Hundreds of Useful Hand-picked Code Snippets -- by Apress &amp; many self-published under the publishing label Fluo~

Updated on December 16, 2020

Comments

  • Dr.Kameleon
    Dr.Kameleon over 3 years

    OK, I know that in C++ a - let's say 2-dimensional - array can be initialized this way :

    int theArray[5][3] = { 
        {1,2,3},
        {4,5,6},
        {7,8,9},
        {10,11,12},
        {13,14,15} 
    };
    

    Now, what if I want to use pre-existing arrays as theArray's elements?

    E.g.

    // A, B, C, D,... have already been declared as :
    // `const U64 A[] = { 1,2,3,4 };` etc...
    
    const U64 multiDimArray[12][64] = { 
         A, B, C, D, E, F,  
         G, H, I, J, K, L
    };
    

    This one, throws an error though :

    cannot initialize an array element of type 'const U64' 
    (aka 'const unsigned long long') with an lvalue of type 'const U64 [64]'
    

    I see the point, but hopefully you can see mine.

    Is there a workaround so that I can easily achieve the same thing? (Any suggestion - perhaps something using Boost? - is welcome)

  • Dr.Kameleon
    Dr.Kameleon over 11 years
    This sounds like a really interesting idea. Let me see how I could fit this into my general code scheme. ;-)
  • Stephen Chu
    Stephen Chu over 11 years
    And you can use the new array class for fixed size array.