[comp.std.c] const char*

berg@marvin.e17.physik.tu-muenchen.de (Stephen R. van den Berg) (05/03/91)

Someone posted something on strchr lately, I couldn't exactly distill what
they/he/she meant.

To make the problem a bit more clear, I have included a real life problem.
What to do with the following program?
---------------------------------------------
char*findblank(const char*p){
 while(*p&&*p!=' ')
   p++;
 return p;}

main(){char*a;
 strcpy(a,"Hello there!");
 *findblank(a)='.';
 puts(a);
 return 0;}
---------------------------------------------
The compiler issues a warning for trying to return a (const char*), which
should have been a (char*).  But from the viewpoint of the function, this
is exactly what is intended.  "const char*p" is meant to be "const" because
the function findblank() does not touch the characters in the string, however,
the function findblank() does not want to impose any restrictions on any
calling programs, i.e. calling programs may very well choose to change
the string.

How do I solve this?  Can it not be solved? (i.e. the warning can not be
avoided, and we have to live with it until compilers get more intelligent and
understand the program they're compiling.)

Should we change the "return p;" into "return (char*)p;"?  (though, that
seems a bit awkward, since we weren't doing anything 'illegal' in the first
place.)
--
Sincerely,                 berg@marvin.e17.physik.tu-muenchen.de
           Stephen R. van den Berg.
"I code it in 5 min, optimize it in 90 min, because it's so well optimized:
it runs in only 5 min.  Actually, most of the time I optimize programs."

gwyn@smoke.brl.mil (Doug Gwyn) (05/04/91)

In article <4278@rwthinf.UUCP> berg@marvin.e17.physik.tu-muenchen.de (Stephen R. van den Berg) writes:
>What to do with the following program?

The same thing as in the strchr() implementation I just posted.

bjoern@drdhh.hanse.de (Bjoern Kriews) (05/08/91)

From article <4278@rwthinf.UUCP>, by berg@marvin.e17.physik.tu-muenchen.de (Stephen R. van den Berg):
> main(){char*a;
>  strcpy(a,"Hello there!");
>  *findblank(a)='.';
>  puts(a);
>  return 0;}
> ---------------------------------------------
> The compiler issues a warning for trying to return a (const char*), which
> should have been a (char*).  

Some compilers will also produce a warning about using 'a' without having
assigned any value to it. :-)

(Yes I know, that's not the point and I'm sure you wouldn't write such
 things in real code, but I just couldn't resist...)
 
Greetings, Bjoern

---
bjoern@drdhh.hanse.de = Bjoern Kriews / Stormsweg 6 / 2000 Hamburg 76 / FRG
"gaaga mahwe Bjoern urgl ufzae Turbo-C bnub"     (J. Willamowius)

andre@targon.UUCP (Andre van Dalen) (05/12/91)

In article <16061@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes:
>In article <4278@rwthinf.UUCP> berg@marvin.e17.physik.tu-muenchen.de (Stephen R. van den Berg) writes:
>>What to do with the following program?
>The same thing as in the strchr() implementation I just posted.

Does this mean that you can transform a const char * by calling a function?
like:

char * un_const (const char *p)
{
	return (char *)p;
}

void do_something(const char *read_only)
{
	char *foo = un_const(read_only);

	*foo = '\0';
}

This would break the clean behaviour that can be forced through
the use of const pointers. Am I right here or did I miss something?

-- 
The mail|    AAA         DDDD  It's not the kill, but the thrill of the chase.
demon...|   AA AAvv   vvDD  DD        Ketchup is a vegetable.
hits!.@&|  AAAAAAAvv vvDD  DD                    {nixbur|nixtor}!adalen.via
--more--| AAA   AAAvvvDDDDDD    Andre van Dalen, uunet!hp4nl!targon!andre

diamond@jit533.swstokyo.dec.com (Norman Diamond) (05/13/91)

In article <1742@targon.UUCP> andre@targon.UUCP (Andre van Dalen) writes:

>Does this mean that you can transform a const char * by calling a function?
>char * un_const (const char *p) { return (char *)p; }
>const char *read_only;
>char *foo = un_const(read_only);

Sure, but why bother coding a function?  You can just say:
 char *foo = (char *) read_only;
--
Norman Diamond       diamond@tkov50.enet.dec.com
If this were the company's opinion, I wouldn't be allowed to post it.
Permission is granted to feel this signature, but not to look at it.

gwyn@smoke.brl.mil (Doug Gwyn) (05/13/91)

In article <1742@targon.UUCP> andre@targon.UUCP (Andre van Dalen) writes:
>Does this mean that you can transform a const char * by calling a function?
[...]
>This would break the clean behaviour that can be forced through
>the use of const pointers. Am I right here or did I miss something?

You missed a lot!  You don't need to call a function in order to
convert type "const char*" to type "char *"; a cast will suffice.
The "const" in the strchr() parameter type does not mean what you
think it does, either.  Finally, no "illegal" action occurs just
by conversion of the pointer types, but rather only when the
pointer to non-const is used in an attempt to modify what it
points to, if what is pointed to is really a const-qualified object.