4
char a[][4] = {{'a','a','a'},
               {'a','a','a'},
               {'a','a','a'},
               {'a','a','a'}};

In c++ you must initialize the last index of an array.

In java if you do this it will result in an error.

Can someone explain me why??

Also when I check the length of array like this in java.

System.out.println(a[0].length);

the result is the length of the column in the array

lets says there is an '\0' at the end of array and when I check the length of array in c++ like this.

cout << strlen(a[0]) 

I get the length of an entire array.

Can I just get the lenght of the row or column in an array in c++ like in java?

Is this becuase array in java is an object?

2 Answers2

10

In Java, a multidimensional array is an array that contains arrays (and a multidimensional array variable does not necessarily contain a multidimensional array, i.e. it could be null). In C++, a multidimensional array variable is itself a single gigantic array, created instantly, with its multidimensional nature only being syntactic sugar. In C++, you have to initialize all but the first index of an array in order to tell the compiler how to convert between your multidimensional array syntax and the single gigantic array that it is actually using behind the scenes.

You can get more Java-like behavior in C++ by initializing a multidimensional array as a pointer to an array of pointers to arrays, instead of simply as an array of arrays (objects in Java are mostly equivalent to pointers to objects in C++). Of course, then you'd have to worry about memory management, which Java does for you.

Brilliand
  • 13,404
  • 6
  • 46
  • 58
  • 7
    Or you could stop leading an asketic life and use std::vector or std::array :) – ScarletAmaranth May 14 '12 at 22:47
  • Good point. Those also provide a size() function that does the same thing as Java's .length. – Brilliand May 14 '12 at 22:51
  • It is important to note that where this answer says *single gigantic array* it means *single gigantic block of memory*. Arrays are both the memory that they take and the type, and while the compiler will allocate a single block, it will not create any hidden *array* (as a type) – David Rodríguez - dribeas May 14 '12 at 23:18
2

There is a simple template that can be used to have the compiler give you the size of an array:

template <typename T, size_t N>
size_t array_size( T (&)[N] ) {
   return N;
}

That template can be used with the whole array, or with the contained array:

char a[][4] = {{'a','a','a'},
               {'a','a','a'},
               {'a','a','a'},
               {'a','a','a'}};
std::cout << array_size(a)    << std::endl;    // 4
std::cout << array_size(a[0]) << std::endl;    // 4

In case you wonder why both yield the same result, the sizes of the array are row first and then column. In your case you are initializing 4 rows with three elements (the fourth element will be value initialized, i.e. zeroed). If you meant to have 4 rows of 3 columns, your definition should be: char a[][3].

David Rodríguez - dribeas
  • 204,818
  • 23
  • 294
  • 489