www.digitalmars.com         C & C++   DMDScript  

c++ - Multi-Dimensional Array Confusion

I was just flying over the D specification to get familiar with
the language and got confused with something regarding arrays of
character strings. It might have been discussed here before, it
might have been answered in some other part of the documentation
that I haven't reach yet, or I might have a completely wrong
understanding of these matters in the first place, so please
forgive my ignorance.

On the language reference page for 'Arrays' in section 'Array
Declarations' it states that "int[4][3]" creates an array of 3
arrays of 4 ints each. This seems to be different from the C++
programming standard (chapter 2.3.6 Arrays), which states that "int
[4][3]" creates an array of 4 arrays with 3 ints each.

I realize that, technically this doesn't matter, as elements
always end up on the same address (assuming linear memory layout),
and I think the D definition makes more sense. In fact, I always
thought it to be like that in C++ as well and never really thought
about it - until today. The confusion starts when considering the
declaration of arrays of character strings.

In C++, the expression "char[][3]" declares a variable length
array of strings with 3 characters each, which can easily be
verified using

  char c[][3] =
  {
      { "he" },
      { "ll" },
      { "ow" },
      { "or" },
      { "ld" }
  };

According to the D specification (page 'Expressions', section 'New
Expressions') and in accordance with the definition of arrays, the
same expression declares an array of 3 strings with variable
length.

It seems to me that the treatment of multi-dimensional arrays is
different between D and C++. Is this correct, and, if yes, should
it be pointed out in the documentation, if not already done?
Jan 08 2007