Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!nuchat!moray!urchin!p6.f506.n106.z1.fidonet.org!Bob.Stout From: Bob.Stout@p6.f506.n106.z1.fidonet.org (Bob Stout) Newsgroups: comp.lang.c Subject: Re: index and rindex question... Message-ID: <11716.25C6818B@urchin.fidonet.org> Date: 30 Jan 90 16:11:39 GMT Sender: ufgate@urchin.fidonet.org (newsout1.26) Organization: FidoNet node 1:106/506.6 - Fulcrum's Edge, Spring TX Lines: 15 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: #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.