index and rindex question...

Bob Stout Bob.Stout at p6.f506.n106.z1.fidonet.org
Wed Jan 31 03:11:39 AEST 1990


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. 



More information about the Comp.lang.c mailing list