[comp.lang.c] const * question

psc@lznv.ATT.COM (Paul S. R. Chisholm) (10/17/87)

<In Syracuse, you dress in a tie; in Ephesus, you juggle . . . or die!>

I really like the idea of const pointers.  I think I even understand
them:  "const char *p" is a pointer to a constant string, "char *const
p" is a constant pointer to a string, and "const char *const p" is a
constant pointer to a constant string.  But I've stumbled a case where
either I'm confused, or my compiler is.

Please consider the following routine:

char *
next( const char *s )
{
	const char	*p;

	p = s + 1;
	return p;
}

The function next() is not allowed to modify the contents of the string
passed to it.  (It's irrelevant whether it changes s or not.  s is a
function argument, and thus, just a copy of the pointer.)  It's
appropriate for p to be a pointer to constant, too, since it points to
(some of) the same data.

Here's the rub:  my compiler (Turbo C) complains that the return
statement is a suspicious pointer conversion.  Well, I can understand
that, sort of:  it's converting from a const char* to a char*, and
most of the time, that's an attempt to modify constant data you're
passed a pointer to.  But it's not appropriate for next() to return
(const char*); after all, the caller may very well be allowed to
modify the data it passed a pointer to!

Have I missed an important point?  Have the compiler writers confused
return conversion semantics with assignment return semantics in this
case?  Which needs the fix:  my code, or their compiler?

-Paul S. R. Chisholm, {ihnp4,cbosgd,allegra,rutgers}!mtune!lznv!psc
AT&T Mail !psrchisholm, Internet psc@lznv.att.com
I'm not speaking for my employer, I'm just speaking my mind.

guy%gorodish@Sun.COM (Guy Harris) (10/18/87)

> Have I missed an important point?  Have the compiler writers confused
> return conversion semantics with assignment return semantics in this
> case?  Which needs the fix:  my code, or their compiler?

Neither, technically; you are trying to get the language to let you do
something reasonable, and the rules of the language - which are being correctly
interpreted by the compiler - won't let you.  In effect, you want a
polymorphous function that takes a "const char *" argument and returns a "const
char *" value, and takes a "char *" argument and returns a "char *" value;
however, C doesn't support polymorphism.
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com

gwyn@brl-smoke.ARPA (Doug Gwyn ) (10/19/87)

In article <1151@lznv.ATT.COM> psc@lznv.ATT.COM (Paul S. R. Chisholm) writes:
>char *
>next( const char *s )
>{
>	const char	*p;
>	p = s + 1;
>	return p;
>}
>Here's the rub:  my compiler (Turbo C) complains that the return
>statement is a suspicious pointer conversion.

It IS "suspicious", but not necessarily erroneous, depending on
what s actually pointed to and what you will subsequently do with
p.  I recommend writing
	p = (char *)s + 1;
to indicate that you know about the constness and are deliberately
ignoring it.

All that const on a formal function parameter means is that the
function itself shall not use that pointer to modify data, not
that the data pointed to is necessarily non-modifiable.  This is
a subtle but important distinction.