[2674] in Kerberos_V5_Development
Re: const char * -> char *
daemon@ATHENA.MIT.EDU (Jeffrey Hutzelman)
Tue Oct 28 01:30:27 1997
Date: Tue, 28 Oct 1997 01:29:59 -0500 (EST)
From: Jeffrey Hutzelman <jhutz+@cmu.edu>
Reply-To: Jeffrey Hutzelman <jhutz+@cmu.edu>
To: Ken Hornstein <kenh@cmf.nrl.navy.mil>
Cc: krbdev@MIT.EDU
In-Reply-To: <199710280532.AAA24957@ginger.cmf.nrl.navy.mil>
> Okay ... _another_ C standard question, about that bastard system from
> hell known as Unicos :-/
>
> The Unicos system I'm using gives an error if you have an argument
> of "const char *" to a function that takes "char *". This happens
> in a number of places in the krb5 source tree ... most commonly in
> calls to gethostbyname(). Two questions:
>
> - Is this "correct" behavior? (So I know whether or not to submit a
> compiler bug).
Yes, this is correct behaviour. The "const" qualifier means that you're
promising not to modify that string (note that 'const' applies to the
characters pointed to, not the pointer). If you pass such a pointer
to a function whose argument is not also 'const', then you might be
breaking that promise. Thus, it is an error to "discard" the const-ness
of something when you pass it to another function.
> - What's the right fix? I've been just putting in an explicit cast
> to char *.
>
> (And before you say anything Assar ... Heimdal suffers from the exact
> same problem :-) ).
Well, the right fix is for all functions that really don't modify
things (most notably, everything in the standard library) to declare
the relevant arguments 'const', so you can safely pass things to them.
Or, better yet, eliminate 'const' from the language - IMNSHO it's
always been more trouble than it's worth.
-- Jeff