ANSI C standard library

Doug Gwyn gwyn at smoke.brl.mil
Sat May 4 06:47:26 AEST 1991


In article <116105 at tut.cis.ohio-state.edu> meranda at iguana.cis.ohio-state.edu (deron meranda) writes:
>Concerning strchr() and const char *'s...
>It appears as if you are correct.  The Rationale 3.3.4 clearly
>states that a (const char *) may be typecast to a (char *).
>However, it also appears that one can not even dereference the
>resulting pointer, whether or not the object is modified
>(actually the behavior is undefined).

As I recall the constraints, you can certainly access the pointer-to
object for reading, and unless the actual object itself is const-
qualified, you can also access it for writing.  The "const" in a
"const char *" parameter does NOT mean that the pointed-to chars
have to be read-only; rather, it means that the function cannot use
that parameter to modify them.

I see no problem in implementing strchr() using strictly conforming code:

char *strchr( const char *s, int c )
	{
	do
		if ( *s == (char)c )
			return (char *)s;
	while ( *s++ != '\0' );

	return (char *)0;
	}



More information about the Comp.std.c mailing list