Re: Why is "Hello World" const char* ?

From: Jack Klein (jackklein_at_spamcop.net)
Date: 02/06/05


Date: Sat, 05 Feb 2005 18:40:47 -0600

On 5 Feb 2005 12:55:31 -0800, "Luke Wu" <LookSkywalker@gmail.com>
wrote in comp.lang.c:

>
> john_bode@my-deja.com wrote:
> > Hans wrote:
> > > Hello,
> > >
> > > Why all C/C++ guys write:
> > >
> > > const char* str = "Hello";
> > >
> >
> > C++ guys don't use char arrays or pointers to char for text data if
> > they can help it; that's what the std::string datatype is for.
> >
> > > or
> > >
> > > const char[] str = "Hello";
> > >
> > > Why is this not a more elegant way:
> > >
> > > const unsigned char* str = "Hello"; (or [])
> > >
> >
> > Because the type of a string literal is "const char *", not "const
> > unsigned char*".
> >
>
> Your statement is incorrect. Neve A string literal is an array of
> (const char) representing each of the characters within quotation " "
> and an extra element thereafter to terminate it: NUL. rtheless, when
> using a string literal in quotations " " in value contexts, like
> assignments, C automatically replaces the string literal (which at this
> point in runtime or compile time is guaranteed to be allocated already)
> with a pointer value pointing to its first character (to take part in
> the expression). Since the first character is of type (const char) as
> we mentioned earlier, the type of the pointer value is (const char *)-
> pointer to const char.

Since you are talking about C specifically here, you are just plain
wrong. The type of a string literal is "array of char", NOT "array of
const char". The address of a string literal has the type "pointer to
char", NOT "pointer to const char".

Either you think the rules of some other language apply to C, or you
just don't know C as well as you think you do.

> Try not to say "a string literal is a pointer to (const) char," because
> it is an incorrect statement (even though people will understand it,
> they might think less of you).

Right, both because a string literal is not a pointer, and because its
address is a pointer to char, not a pointer to const char.

-- 
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html


Relevant Pages

  • Re: Why is "Hello World" const char* ?
    ... > Your statement is incorrect. ... Since the first character is of type (const char) as ... > pointer to const char. ... The type of a string literal is "array of char", ...
    (comp.lang.cpp)
  • Re: extern const char * vs. extern const char []http://tinyurl.com/47e3k
    ... >creates a pointer to a 'C' string literal. ... >array and it has a location, ... many platforms) since your const char* is a different type from the ... extern variables in the other TU - you need const charas the type ...
    (comp.lang.cpp)
  • Re: declaration error
    ... 'message1' is the name of an array of const char. ... msgptr12has type 'pointer to array of const char'. ...
    (comp.lang.c)
  • Re: string literals question
    ... The type of a string literal in C is array of char, ... When converted to a pointer, ... not pointer to const char. ... Attempting to modify a string literal in C is undefined behavior not ...
    (comp.lang.c)
  • Re: 5.1.2.2.1 argv misdescription [was Re: Please Help---------&
    ... argv array" is incorrect, it should be changed to "the array that argv ... parameter of every function in the standard library which is a pointer ... incorrect, and not merely imprecise. ...
    (comp.std.c)