Thoughts on `const' parameters

Stephen J. Friedl friedl at vsi.COM
Wed Nov 30 10:58:58 AEST 1988


Hi folks,

     I would like a little thoughtful input on something
related to the const type qualifier.  I have been using it
heavily and am a little nervous about what seems to me to
be questionable usage.

     Let's say that I have a function like strchr(), which
might be written as:

	char *strchr(const char *string, int ch)
	{
		do
		{
			if (ch == *string)
				return((char *)string);
		} while (*string++);

		return(NULL);
	}

     What prevents me from passing a const argument (say, a string
in readonly memory), locating my desired character within the string,
and then storing a NUL there?  Nothing in the declaration prevents
this, and I've not done anything illegal like playing cast games.
If I were a paranoid.library.function.writer, presumably, one could
do one of:

	const char *strchr(const char *string, int ch)
or
	char       *strchr(char       *string, int ch)

but obviously the Fine Folks At X3J11 have thought a lot about
this; I'd like to hear the reasoning for this kind of thing.

     Note that I'm not objection to the case of strchr() here, as
it's part of the standard and it's part of history.  I am thinking
here of the general case of cheating the const mechanism.

     Steve

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

-- 
Steve Friedl    V-Systems, Inc.  +1 714 545 6442    3B2-kind-of-guy
friedl at vsi.com     {backbones}!vsi.com!friedl    attmail!vsi!friedl
---------Nancy Reagan on cutting the grass: "Just say mow"---------
:wq!



More information about the Comp.std.c mailing list