index and rindex question...

Karl Heuer karl at haddock.ima.isc.com
Fri Feb 2 03:33:33 AEST 1990


In article <11716.25C6818B at urchin.fidonet.org> Bob.Stout at p6.f506.n106.z1.fidonet.org (Bob Stout) writes:
>Based on this usage, I use:
>#define index(s,c)  ((strchr((s),(c))) ? (size_t)(strchr((s),(c))-(s)) : -1)
>#define rindex(s,c) ((strrchr((s),(c))) ? (size_t)(strrchr((s),(c))-(s)) : -1)
>
>Note that these are *not* safe macros since the `s' argument may be
>evaluated three times and the `c' argument twice.

They also do twice as much work, and would be confusing to anyone who expects
those names to have the V7/BSD semantics.  Wouldn't it be simpler to just use
strchr and strrchr, which should be present in any modern system?

Karl W. Z. Heuer (karl at ima.ima.isc.com or harvard!ima!karl), The Walking Lint



More information about the Comp.lang.c mailing list