1

I am assigning a 3D array via an initializer list to a struct, to take advantage of member-wise copy. I was interested in doing something like what was described in this post about array copying

This seems to work, but it can't be done without adding an extra set of brackets:

struct Options
   {
   char array [20][20][20];
   };

int main(void)
   {
   Options K = {{ { "-x", "-o" }, { "-y", "-p", "-q" }, {"-r", "-s"} }};
   };

The outermost brackets are to encapsulate the value being assigned. The next pair in is the extra pair I can't explain. The 3 dimensions to be assigned are:

  • x = number of bracketed groups of strings
  • y = number of strings in each group
  • z = number of characters in each string

This would normally not worry me because everything is copied. However, after assignment, innermost values that should be contiguous in memory are spaced apart by multiples of an extra dimension. Instead of "-x" and "-o" being separated by 20 bytes, for example, they are actually separated by 20 * 20 bytes. I'm confused. How do I get this to work without adding an extra dimension?

Community
  • 1
  • 1

1 Answers1

0

To answer your question directly in your code:

Options K = {  // struct
              { // array 
                 {  // array[0]
                     "-x", // array[0][0] with 3 chars, as string literal 
                     {     // array[0][1] with 3 chars, transformed in braced form by me
                        '-',  // array[0][1][0]
                        'o',  // array[0][1][1]
                        '\0'  // array[0][1][2]
                      } 
                 },
                 {  // array[1]
                    "-y", "-p", "-q" 
                 },
                 {  // array[2]
                    "-r", "-s"
                 }
               }
            };

If you output this array, you'll naturally get something like (replaceing non printable '\0' with dots:

- x . . . . . . . . . . . . . . . . . .   // A first 2D array contained in array[0]
- o . . . . . . . . . . . . . . . . . .   // note that each line is exactly 20 chars long
. . . . . . . . . . . . . . . . . . . . 
(followed by 17 similar empty lines)
- y . . . . . . . . . . . . . . . . . .   // A second 2D array contained in array[1] 
- p . . . . . . . . . . . . . . . . . . 
- q . . . . . . . . . . . . . . . . . . 
(etc)

Live demo

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Does the second pair in simply correspond to the array structure itself, without respect to dimension? I think that's what threw me. With your transformation, it shows the brace count is 4 for 3 dimensions, and I always thought of the number of braces equaling the number of dimensions. – user2069339 Sep 03 '15 at 01:19
  • For array initialization you have indeed 1 brace level per dimension, which contains the element of that dimension. Here you come to 4 because on the top of the 3 dimensions you have the struct. The tricky thing with char arrays is that they can be represented by a string litteral without need for an extra brace. – Christophe Sep 03 '15 at 06:31