Re: Why is "Hello World" const char* ?
From: Jack Klein (jackklein_at_spamcop.net)
Date: 02/06/05
- Next message: Jack Klein: "Re: Why C Is Not My Favourite Programming Language"
- Previous message: Jack Klein: "Re: Why is "Hello World" const char* ?"
- In reply to: Luke Wu: "Re: Why is "Hello World" const char* ?"
- Next in thread: Lawrence Kirby: "Re: Why is "Hello World" const char* ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Jack Klein: "Re: Why C Is Not My Favourite Programming Language"
- Previous message: Jack Klein: "Re: Why is "Hello World" const char* ?"
- In reply to: Luke Wu: "Re: Why is "Hello World" const char* ?"
- Next in thread: Lawrence Kirby: "Re: Why is "Hello World" const char* ?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|