When is a cast not a cast?

phil at ux1.cso.uiuc.edu phil at ux1.cso.uiuc.edu
Thu May 25 03:15:00 AEST 1989


What about:

    long x,y,z,*q,*p,a[19];
    int i;
      .
      .
    p = a;
    q = NULL[i];  /* or even i[NULL] */
    x = *(p+q);
    q = q[1];
    y = p[q];
    q = q[1];
    z = q[p];

Obviously this is machine dependent.  But it might be useful on most machines
anyway since most implement NULL as 0.  Thus "q" will be of type pointer, but
will in fact have an OFFSET instead of an absolute value.

When I program in assembler, I usually index with offsets instead of with
integers, so I can avoid the repeated multiplies each time around the loop
that increments the offset or integer.  Thus:

    q = q[1];

might be faster depending on how well the compiler recognizes the usage of the
constant.  By generating code that directly adds the length of a "long" to the
value of "q", you avoid all the multiplies.

Alternatively in many cases, one can just increment absolute pointers as well.
There are a few cases where the same offset can be used in more than one array
because the arrays have elements of the same length/type.

--Phil howard--  <phil at ux1.cso.uiuc.edu>



More information about the Comp.lang.c mailing list