Divide and C

Duane Richard LaMont lamont at uts.amdahl.com
Fri Mar 29 15:20:12 AEST 1991


In article <1991Mar27.185804.7221 at uunet.uu.net> karln!karln at uunet.uu.net (Karl Nicholas) writes:
>    EX: int array[10][10];
>        val = 24;
>        ++array[val/10][val%10];
>
>    This is the only way I know how to do this.
>    My problem with it is that is requires TWO divides.
>    
>    Wanting my program to run almost twice as fast, I wrote an ASSEMBLY
>    lang subroutine, to be called from C.

Considering that you've already resorted to assembly language, the
following hack shouldn't bother you.  Provided that the array is
defined as in your example with both dimensions specified, you could
use:

	++((int *) array)[val];

or even:

	++*(*array + val);

However, if your program uses arrays of pointers to arrays of
integers, the above won't work and there is no counterpart solution.
See the FAQ if you don't know what I'm talking about.


Rick LaMont



More information about the Comp.lang.c mailing list