Thoughts on `const' parameters

Doug Gwyn gwyn at smoke.BRL.MIL
Fri Dec 2 04:08:37 AEST 1988


In article <957 at vsi.COM> friedl at vsi.COM (Stephen J. Friedl) writes:
>	char *strchr(const char *string, int ch)

The reason there is a "const" in the first parameter is that it documents
the interface requirement that strchr() is not permitted to modify data
through that pointer.  The reason the result type does NOT have "the
const" qualifier is that having it there would prohibit using the returned
pointer to modify anything.

Whether data is really constant depends on whether it was defined with
the const attribute.  Pointers to qualified types are representation-
compatible with pointers to non-qualified types, so any style of char*
can be passed as the first argument to strchr().

>P.S. - did anybody consider putting `index' and `rindex' into the standard?

Not for very long.  They have been obsolete since 1980, although
systems based on obsolete C technology (such as 4BSD) continue to
provide them.  Use strchr() and strrchr() instead.



More information about the Comp.std.c mailing list