Topic: A language feature or quirk ?
Author: Jarand.Roynstrand@efi.sintef.no
Date: 17 Mar 1995 16:35:55 GMT Raw View
In article <3k4l2a$cbu@fsgm01.fnal.gov> b91926@fsgm01.fnal.gov (David Sachs) writes:
Path: due.unit.no!trane.uninett.no!sunic!sunic.sunet.se!doc.news.pipex.net!pipex!howland.reston.ans.net!news.moneng.mei.com!uwm.edu!lll-winken.llnl.gov!fnnews.fnal.gov!fsgm01.fnal.gov!not-for-mail
You have run into one of the less fortunate features of C++.
The string "Hello" is of type char*, not const char*, as would be
logical. There is undoubtedly too much (mostly C language) code
around that would be broken if the C++ language were changed to
treat a string constant as const.
Does this mean that the standard require it to be leagal to modify
a string constant?
Would a system that gave a segmetation error on the construct
char *p ="Hello!";
p[0] = 'B';
be non-conforming?
(Please email a copy of responses to me. I am leaving office for some time.)
--
==== Best regards
\\ Jarand Roeynstrand
\\ DIG-hackers department
\====\
.,;,\ \ EFI
,;;;;;;;;,_/ ____ N-7034 Trondheim
| | Norway
| |
\__/
Phone: +47-73 59 72 75 Email: jr@efi.sintef.no
Author: padriff@fudriff.demon.co.uk ("D:WINSOCKKA9QSPOOLMAIL")
Date: Thu, 16 Mar 1995 03:45:15 +0000 Raw View
In article: <3k2tn6$jig@mozo.cc.purdue.edu> kavuri@lips.ecn.purdue.edu (Surya N Kavuri )
writes:
> void Print(char *s) { cout << s; }
>
> int main ()
> {
> Print("Hello"); // compiles; not an error!
> const char* s = "Hello";
> Print(s); // Compile time error as expected!
>
> cout << "What on earth is going on ?" << endl;
> return 0;
> }
I'm not sure about this but the way I think it works is this :
This doesn't produce an error:
> Print("Hello"); // compiles; not an error!
because although you cannot change this string as it is hard coded in it is a pointer to the
memory location is passed to the function Print() and this is not actually a const char *.
I say this because you can quite happily have :
char * s = "Hello";
if it was the case that any pointer to a hard coded string was const then this wouldn't work.
If I am wrong, someone please correct me!
--
Paddy McDonald
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ ACCEPT NOTHING | SMILE ~
~ BLAME EVERYONE | DON'T FRET ~
~ BE BITTER | DRINK COFFEE ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
padriff@fudriff.demon.co.uk
Author: pstaite@powertool.rchland.ibm.com (Philip Staite)
Date: 16 Mar 1995 14:19:21 GMT Raw View
In article <3k2tn6$jig@mozo.cc.purdue.edu>, kavuri@lips.ecn.purdue.edu (Surya N Kavuri ) writes:
|>
|> void Print(char *s) { cout << s; }
|>
|> int main ()
|> {
|> Print("Hello"); // compiles; not an error!
|>
|> const char* s = "Hello";
|> Print(s); // Compile time error as expected!
|>
|> cout << "What on earth is going on ?" << endl;
|> return 0;
|> }
Const correctness is going on :-)
To the compiler, "Hello" is a char*. You can assign a char* to a const char*, as you do to s. Now, s being a const char* you're not allowed to do anything that may change the chars s points to.
The Print function takes a char*, not a const char*. If Print took a const char* it would promise not to change the chars pointed to by its argument. However, Print makes no such promise. Therefore the compiler cannot take the chance that Print will violate the constness of s.
--
Phil Staite
pstaite@vnet.ibm.com
Author: "Ronald F. Guilmette" <rfg@rahul.net>
Date: 17 Mar 1995 09:44:40 GMT Raw View
In article <3k2tn6$jig@mozo.cc.purdue.edu>,
Surya N Kavuri <kavuri@lips.ecn.purdue.edu> wrote:
>
> void Print(char *s) { cout << s; }
>
>int main ()
>{
> Print("Hello"); // compiles; not an error!
>
> const char* s = "Hello";
> Print(s); // Compile time error as expected!
>
> cout << "What on earth is going on ?" << endl;
> return 0;
>}
Character string literals (e.g. the first "Hello" above) have type char[]
which decays (almost immediately, and in most contexts) to type `char*'.
They DO NOT have type `const char[]' and thus they DO NOT decay into
type `const char*'.
The reason is historical, and is a matter of being backward compatible
with ANSI/ISO C.
--
-- Ron Guilmette, Sunnyvale, CA ---------- RG Consulting -------------------
---- E-mail: rfg@segfault.us.com ----------- Purveyors of Compiler Test ----
-------------------------------------------- Suites and Bullet-Proof Shoes -
Author: rnn@morris.NoSubdomain.NoDomain (Rajesh Nihal)
Date: 14 Mar 1995 16:09:54 GMT Raw View
In article <3k2tn6$jig@mozo.cc.purdue.edu>, kavuri@lips.ecn.purdue.edu (Surya N Kavuri ) writes:
|>
|> void Print(char *s) { cout << s; }
|>
|> int main ()
|> {
|> Print("Hello"); // compiles; not an error!
|>
|> const char* s = "Hello";
|> Print(s); // Compile time error as expected!
|>
|> cout << "What on earth is going on ?" << endl;
|> return 0;
|> }
|>
|>
|>
|>
|> Surya Kavuri
You need to cast the s to char*, since s is declared as const char* and
Print function takes its argument as char*. In C++ const char* do not
get automatically casted to char*.
Raj.
Author: b91926@fsgm01.fnal.gov (David Sachs)
Date: 14 Mar 1995 11:51:06 -0600 Raw View
kavuri@lips.ecn.purdue.edu (Surya N Kavuri ) writes:
>
> void Print(char *s) { cout << s; }
>
>int main ()
>{
> Print("Hello"); // compiles; not an error!
>
> const char* s = "Hello";
> Print(s); // Compile time error as expected!
>
> cout << "What on earth is going on ?" << endl;
> return 0;
>}
You have run into one of the less fortunate features of C++.
The string "Hello" is of type char*, not const char*, as would be
logical. There is undoubtedly too much (mostly C language) code
around that would be broken if the C++ language were changed to
treat a string constant as const.
Author: mueller@garwein.hai.siemens.co.at (Harald M. Mueller)
Date: Wed, 15 Mar 1995 13:04:14 GMT Raw View
In article jig@mozo.cc.purdue.edu, kavuri@lips.ecn.purdue.edu (Surya N Kavuri ) writes:
|>
|> void Print(char *s) { cout << s; }
|>
|>int main ()
|>{
|> Print("Hello"); // compiles; not an error!
|>
|> const char* s = "Hello";
|> Print(s); // Compile time error as expected!
|>
|> cout << "What on earth is going on ?" << endl;
|> return 0;
|>}
Because of good old C, string literals are of type char* and not const char*, so
that you can write:
char *s = "Hello";
which would be impossible if "Hello" was a const char*.
HMMueller
== --------------------------------------------------------------
== Dr. Harald M. Mueller mueller@garwein.hai.siemens.co.at
== Siemens AG Austria Siemens AG Austria
== PSE EZE TNA1 Erdberger Laende 26
== fax: +43-1-71711-5425 A-1030 Vienna/Austria
== --------------------------------------------------------------
Author: kavuri@lips.ecn.purdue.edu (Surya N Kavuri )
Date: 14 Mar 1995 02:06:30 GMT Raw View
void Print(char *s) { cout << s; }
int main ()
{
Print("Hello"); // compiles; not an error!
const char* s = "Hello";
Print(s); // Compile time error as expected!
cout << "What on earth is going on ?" << endl;
return 0;
}
Surya Kavuri