When it is amoral... (Re: When is a cast not a cast?)

Norman Diamond diamond at diamond.csl.sony.junet
Mon May 8 13:46:29 AEST 1989


In article <563 at lzaz.ATT.COM> hutch at lzaz.ATT.COM (R.HUTCHISON) writes:

>Re: addition of two pointers
>
>How 'bout in a binary search to find the middle char in an array of
>characters?
>
>	midpoint_pointer = (start_pointer + end_pointer) / 2;

Yup, and if you have an array of ints?  Let's see, half-way between
the 1st and 6th elements, we have the 3.5th element.

Well yeah, you've got chars not ints.  Only the machine uses word
addressing so that your pointers use some extra bits to indicate
the offset within a word.

If you're planning to leave your present employer before they ever
have to port your code to another machine, you could do this:

   midpoint_pointer = (char *) (((unsigned long) start_pointer +
      (unsigned long) end_pointer) / 2);

--
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.co.jp at relay.cs.net)
  The above opinions are my own.   |  Why are programmers criticized for
  If they're also your opinions,   |  re-inventing the wheel, when car
  you're infringing my copyright.  |  manufacturers are praised for it?



More information about the Comp.lang.c mailing list