Why is "Hello World" const char* ?

C

CBFalconer

Ron said:
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.
 
M

msalters

Ron said:
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.

No, it isn't. In C++ it is definitely an array of N const char, see
2.13.4
and overloading will show that. There is a deprecated limited
conversion
to non-const char*.
Furthermore, even in sizeof( "literal") the type is not const char*,
this will be the true size of the literal object.

( restricted to clc++)

HTH,
Michiel Salters
 
R

Ron Natalie

msalters said:
No, it isn't. In C++ it is definitely an array of N const char, see
2.13.4

You're right of course. When I wrote the "same in C++" I was refering
to the type being an ARRAY, not a pointer as the previous poster referred to.
 
L

Lawrence Kirby

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*".

As others have said its type is array of char. But the fact that it is
char and not unsigned char is significant.

Another thing that is just as significant is the fact that standard
library functions that take or generate strings have arguments/return
values derived from type char, not unsigned char.
Secondly, the plain, unqualified char type may be signed or unsigned, or
a mixture of both, depending on the implementation. The char type is
not treated exactly the same as other signed and unsigned integral
types.

I'm not sure what you mean by a "mixture of both". For any particular
implementation plain char must behave exclusively like either signed char
or unsigned char, except that it is a different type. Think of it like int
and long. On some implementations they have the same representation but
even when that is true they are different types.

Lawrence
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top