Why does C not support an array of columns?

Is this just a random design choice, or is there any specific reason that C supports the major row rather than the major column? I know Fortran uses a column. So what is the reason (if any) for these design options?

+7
source share
3 answers

Based on my answers to some comments on the question, as well as some other answers (and some of my own thoughts - but, in particular, not knowing any C language development process ...), I believe that this is a choice based only on the fact that people making this decision (Ritchie?) needed themselves.

If you interpret the indices of a multidimensional array as matrix indices, it makes sense to have the first index as the row index, and the second as the column index, i.e. column structure. If your applications will be heavy in linear algebra or other matrix matrix computations, it also makes sense to store these structures in such a way as to efficiently move them one column at a time, as many algorithms do. For this reason, programming languages ​​such as Matlab and Fortran benefit from being the main columns - this makes it easy to write efficient code with matrices and matrix algorithms.

C, on the other hand, is much more versatile than, for example, Matlab or Fortran. Unless you intend to use int** specifically for matrices, it doesn't really matter which index it is. And naturally, if a is int** , then a[2] returns int* and a[2][1] returns int - you "dig deeper" into a multidimensional array. For efficiency, we now only care that if we select a[2] and want iteration, it must be cached efficiently. It doesn’t matter if you, a programmer, associate a[2] with a matrix row or column of a matrix - we do not work with matrices!

So there is no strong case (which I can make out from my head) for C to be primary. During the implementation of the first versions, it might have been easier to do this with a large number of lines - perhaps because the low-level base language (assembler?) Was already large in lines - and it was.

+7
source

The elements of the array C guarantee that they are adjacent memory elements, and the 2-dimensional array is an array of arrays, so let's say for the array int a[10][20] ; a[0] itself is an array, and its elements must be adjacent. Similarly, a[0] adjacent to a[1] .

+2
source

C defines only arrays and allows array elements to be arrays in turn. For an array of arrays, the first index selects the element of the array, and the second index selects the element of value in this array. To change their meaning, you will create an illogical grammar.

Interpreting the first index as the row number and the second index as the column number for some 2D matrix data structure is just an interpretation.

Note that Fortran arrays (major columns) are not indexed using two separate index operators.

EDIT: To give an authoritative quote, the C standard says in 6.5.2.1 (C99), explaining how the result of signing a multidimensional array is an n-1-dimensional array

It follows that the arrays are stored in lowercase order

(emphasis mine)

0
source

All Articles