index and rindex question...
Conor P. Cahill
cpcahil at virtech.uucp
Thu Feb 1 23:42:28 AEST 1990
In article <11716.25C6818B at urchin.fidonet.org> Bob.Stout at p6.f506.n106.z1.fidonet.org (Bob Stout) writes:
>In an article of <27 Jan 90 02:04:19 GMT>, (Conor P. Cahill) writes:
>
> >They are equivalent to the system V functions strchr() and strrchr(),
> >respectively.
>
> Both the strchr() and strrchr() functions made it into the ANSI spec while
>index() and rindex() didn't. I believe this was because the latter two
>functions on some systems return an int offset of the character rather than a
>pointer to it. Based on this usage, I use:
The original poster has asked about the "BSD" functions index() and rindex().
The documentation as far back as 4.1BSD shows that they are the equivalent
to strchr()/strrchr() (i.e. they return pointer to char).
>#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)
This would work to replace index() only when the original software came
from a system that did not use the BSD implementation. I would guess that
it would be rather rare today. If I remember correctly, PWB Unix had an
index() that returned int.
For most of the code that you run into today (yes, I know that there are
still V6 and PWB systems around, in fact I work on one every once in a
while) the following defines will suffice:
#define index(s,c) strchr(s,c)
#define rindex(s,c) strrchr(s,c)
--
+-----------------------------------------------------------------------+
| Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 !
| Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 |
+-----------------------------------------------------------------------+
More information about the Comp.lang.c
mailing list