🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #2842

const char*

#2842
From: Stephen R. van den Berg
Date: Fri, 03 May 1991 08:56
37 lines
1903 bytes
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."
Technical Details
Message-ID: <4278@rwthinf.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!unido!rwthinf!marvin.e17.physik.tu-muenchen.de!berg

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!mcsun!unido!rwthinf!marvin.e17.physik.tu-muenchen.de!berg
From: berg@marvin.e17.physik.tu-muenchen.de (Stephen R. van den Berg)
Newsgroups: comp.std.c
Subject: const char*
Message-ID: <4278@rwthinf.UUCP>
Date: 3 May 91 08:56:38 GMT
Sender: news@rwthinf.UUCP
Lines: 37