DEV Community

Discussion on: #001 DS&A - Operators and Arrays

 
pentacular profile image
pentacular

Sure: open-std.org/jtc1/sc22/wg14/www/do... 6.5.2.1 Array subscripting

Lysator is generally confused about arrays -- consider the mess it makes of 2.14 which is a pointer into an array of int *, rather than being multidimensional.

The simplest proof is this.

int array[3][4];
int (*p)[4] = &array[0];
sizeof array[0] == sizeof (int[4]);

Therefore array is an array of length 3 with an element type of int[4].

Note the single dimension used to access it here.

Of course, having accessed array[i], you can now access that one dimensional array via array[i][j], and so on.

Probably the easiest way to think about it is that C provides arrays of arrays, which is one way to implement multi-dimensional arrays, without any of the arrays actually being multi-dimensional.

Thread Thread
 
omarkhatib profile image
Omar

it's all semantics there is no such thing as truly multidimensional arrays as all arrays are one dimensional
you can use a 2 or n dimensional accessor but that's about it.
there is no need to layer a simple thing to look complex.

Thread Thread
 
pentacular profile image
pentacular

There are such things are truly multi-dimensional arrays, just not in C.

For example, in pascal

type
  StatusType = (X, O, Blank);
  BoardType = array[1..3,1..3] of StatusType;
var
  Board : BoardType;

for count1 := 1 to 3 do
  for count2 := 1 to 3 do
    Board[count1, count2] := Blank;

Or in Common Lisp

(let ((a (make-array '(4 3))))
  (aref a i j))

This is not a criticism of C -- deciding to use arrays of arrays rather multi-dimensional arrays is a perfectly reasonable design choice.

It's just a design choice that should be understood clearly. :)