Array Access / Re: ... datum ** and [i][j]

Clay Phipps phipps at fortune.UUCP
Wed Aug 22 10:15:17 AEST 1984

Lines beginning "!?  ": Chris Torek (does he really want to be named ?)
Lines beginning "    ": Clay [editing indicated by -- CP]

!?  let's take a look at a three dimensional [row major -- CP] array.  
!?  Given ``int a[4][6][5];'' as the declaration 
!?  and ``a[2][3][4]'' as the reference, 
!?  we take 2, multiply by six, add three, 
!?  multiply the result by five, and add four, giving 79.

The above calculations are correct.
!?  We can generalize the first as follows:
!?      offset = 0;
!?      for (i = 0, j = num_dimens - 1; i < num_dimensions; i++, j--) {
!?  	    offset = offset + array_index[i];
!?  	    offset = offset * array_sub[j];
!?      }
!?  ... I know it's not optimized C code.  This is just an illustration.  
!?  ... I see some smiles.  You, you look like you want to say something.

Yes, teacher, your program fragment is just plain WRONG.
It gives an incorrect answer, 142,
instead of the correct answer, 79, that was worked out above.

!?  where 
!?  num_dimens		is the number of dimensions in the array,
!?  array_index[]	contains the constants 
!?  			used in the declaration of the array, 

The text above does not describe array "indexes" (or "indices"),
but it does describe the "size" (K&R p. 93) of the dimensions.
To most people, apparently including K&R, 
an "index" is synonymous with a "subscript".
This counter-mnemonic name was a continual source of confusion
while I was trying to work through some examples.
Perhaps this even confused the teacher.
!?  array_sub[]	contains the values used in the reference, and
!?  offset	computes the offset from the base of the storage area.
Assuming that you have followed C style,
and that the above compiler arrays subscripts 0, 1, 2
correspond to the compiled source code subscripts written left to right,
the following is correct for row major arrays in C:
    offset = array_sub[0];
    for (i = 1; i < num_dimens; i++) {
    	offset = offset * array_size[i];
    	offset = offset + array_sub[i];

array_size[]	is what Chris unfortunately named "array_index";
		it contains the constants 
		used in the declaration of the array. 

A better program fragment would be the following:	
    offset = array_sub[0];
    for (i = 1; i < num_dimens; i++) {
    	offset = offset * array_size[i] + array_sub[i];
Note that the code above takes advantage of the fact
that C array declarations, like old-style FORTRAN (IV, 66),
specify a size, instead of lower and upper bounds.
With other languages like PL/I, Pascal, and Full FORTRAN 77,
the dimension size t be calculated, usually at compilation time.

!?  Ok, that's all for today.  Quiz Thursday.  Good night.

I think you had better review Gries, p. 178 (Pratt isn't much help;
don't know about the Dragon Book) before you spring it on us netlanders.

-- Clay Phipps

            { amd  hplabs!hpda  sri-unix  ucbvax!amd }          
   { ihnp4  cbosgd  decvax!decwrl!amd  harpo  allegra}

More information about the Comp.lang.c mailing list