473,795 Members | 3,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is "Hello World" const char* ?

Hello,

Why all C/C++ guys write:

const char* str = "Hello";

or

const char[] str = "Hello";

Why is this not a more elegant way:

const unsigned char* str = "Hello"; (or [])

??

My Reasson: have you ever seen ASCII-Code: -50? The extended ASCII Set
(>127) would be represented by a negative number if you use 'char'
instead of 'unsigned char'.

Thanks!

Hans
Jul 23 '05
23 6641
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.c om, 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

Jul 23 '05 #21

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.


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

Jul 23 '05 #22
On Sat, 05 Feb 2005 12:18:22 -0800, john_bode 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*".


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

Jul 23 '05 #23
msalters wrote:
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.

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.
Jul 23 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
630
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
3
2354
by: lovecreatesbeauty | last post by:
Both `K&R C, 2nd' and `C: A reference manual, 5th' introduce the "hello, world" thing using the name "string-constant". But `ISO/IEC 9899:TC2' does not include this kind of thing in section `A.1.5 Constants'.
10
5361
by: fei.liu | last post by:
Consider the following sample code char * ptr = "hello"; char carray = "hello"; int main(void){ } What does the standard have to say about the storage requirement about ptr and carray? Is it a fair statement that char *ptr will take 4 more bytes (on 32bit platform) in DATA segment? I have found
0
4612
by: devito | last post by:
hi there, for some days i try to build the boost.python tutorial "hello world" without bjam on winxp by using mingw. so i wrote a *.bat-file like the following: // --- snip -------------------------------------------------------------------------------------- @echo off SETLOCAL
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9042
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3723
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.