Re: Why is "Hello World" const char* ?
From: CBFalconer (cbfalconer_at_yahoo.com)
Date: 02/06/05
- Next message: Noah Roberts: "Re: New Promotion, need advice from experience people who deal with this situation."
- Previous message: Victor Bazarov: "Re: pointer confusion :("
- In reply to: Ron Natalie: "Re: Why is "Hello World" const char* ?"
- Next in thread: msalters: "Re: Why is "Hello World" const char* ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 06 Feb 2005 20:46:39 GMT
Ron Natalie wrote:
> Luke Wu wrote:
>
>> In C, a string literal (like "Hello") in value context (like the
>> right side of an assignment= operator) evaluates to a pointer
>> value of type (const char*) that points to the first character.
>
> Nope, it evaluates to an array of char, which converts to pointer
> to char. The characters are effectively const (it is undefined
> behvaior to attempt to change them), but the type of the
> expression is not const char*. (except when sizeof or & is
> applied to the array value).
>
> It's effectively the same in C++, except C++ defines a formal
> array-to-pointer conversion.
However it is advisable to declare such strings as:
const char *foo = "foobar";
so that the C compiler can emit suitable warnings on misuse, such
as trying to alter it with such things as *foo = 'F'; Note that
foo = "barfoo"; is perfectly legal, except you may never find the
"foobar" string again.
-- "If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers." - Keith Thompson
- Next message: Noah Roberts: "Re: New Promotion, need advice from experience people who deal with this situation."
- Previous message: Victor Bazarov: "Re: pointer confusion :("
- In reply to: Ron Natalie: "Re: Why is "Hello World" const char* ?"
- Next in thread: msalters: "Re: Why is "Hello World" const char* ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|