C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

String literals in C++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Neelesh
Guest





PostPosted: Thu Oct 20, 2005 2:04 pm    Post subject: String literals in C++ Reply with quote



Hi,
I have a confusion regarding what is the exact datatype of a "string
literal" in C++. The following code makes the point clear -

#include <string>
using namespace std;

int main()
{
const char *x = "hello"; // allowed
char y[] = "hello"; // allowed
string z = "hello"; // allowed
char *w = "hello" ; // Incorrect, but still g++ allows this.
}

The RHS of all these expressions is same. My questions are

i) In which of these cases a type conversion is involved during the
initialization?
ii) When I just say "hello" as a literal does it represent any
particular type?

Thanks,
~Neelesh


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Victor Bazarov
Guest





PostPosted: Thu Oct 20, 2005 5:24 pm    Post subject: Re: String literals in C++ Reply with quote



Neelesh wrote:
Quote:
I have a confusion regarding what is the exact datatype of a "string
literal" in C++.

It's an array of const char. The size is equal to the number of
characters plus one for the terminating null char plus at least one
for multibyte encoding of each of universal characters. It has static
storage duration.

Quote:
The following code makes the point clear -

#include using namespace std;

int main()
{
const char *x = "hello"; // allowed
char y[] = "hello"; // allowed
string z = "hello"; // allowed
char *w = "hello" ; // Incorrect, but still g++ allows this.

It is not "incorrect". It is "deprecated". Should be supported by all
compilers at this time, though.

Quote:
}

The RHS of all these expressions is same. My questions are

i) In which of these cases a type conversion is involved during the
initialization?

First and fourth, array to pointer. Third, array to pointer and user-
defined (string has a c-tor from const char*).

Quote:
ii) When I just say "hello" as a literal does it represent any
particular type?

Yes, It's "const char[6]".

V

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Alf P. Steinbach
Guest





PostPosted: Thu Oct 20, 2005 5:27 pm    Post subject: Re: String literals in C++ Reply with quote



* Neelesh:
Quote:
I have a confusion regarding what is the exact datatype of a "string
literal" in C++.

array of constant char, or in C++'ish, 'char const [N]'.


Quote:
The following code makes the point clear -

#include using namespace std;

int main()
{
const char *x = "hello"; // allowed
char y[] = "hello"; // allowed
string z = "hello"; // allowed
char *w = "hello" ; // Incorrect, but still g++ allows this.

Actually the last _is_ formally correct, but only due to a C compatibility
feature; it's not something one should do except for interfacing to legacy C
code.


Quote:
}

The RHS of all these expressions is same. My questions are

i) In which of these cases a type conversion is involved during the
initialization?

All of them. Unless the second one doesn't count as a "conversion". There
are so many special meanings of words, and somebody else can look that up!


Quote:
ii) When I just say "hello" as a literal does it represent any
particular type?

'char const [6]', with 6 instead of 5 because of the terminating zero.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Rob
Guest





PostPosted: Thu Oct 20, 2005 7:33 pm    Post subject: Re: String literals in C++ Reply with quote

Neelesh wrote:

Quote:
Hi,
I have a confusion regarding what is the exact datatype of a "string
literal" in C++. The following code makes the point clear -

#include using namespace std;

int main()
{
const char *x = "hello"; // allowed
char y[] = "hello"; // allowed
string z = "hello"; // allowed
char *w = "hello" ; // Incorrect, but still g++ allows this.
}

The RHS of all these expressions is same. My questions are

i) In which of these cases a type conversion is involved during the
initialization?
ii) When I just say "hello" as a literal does it represent any
particular type?


IIRC, the standard requires that a string literal is a const array of
char. However, some older (pre-standard) versions of C did not
support the const keyword, so the type of a string literal is simple
array of char. I'm not sure offhand if draft versions of the C++
standard (before it was ratified) ever required that a string literal
be a const array, but it is probable some older compilers treated a
string literal as a non-const array of char for reasons of backward
compatibility to C.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Rob
Guest





PostPosted: Fri Oct 21, 2005 8:25 am    Post subject: Re: String literals in C++ Reply with quote

Neelesh wrote:

Quote:
Hi,
I have a confusion regarding what is the exact datatype of a "string
literal" in C++. The following code makes the point clear -

#include using namespace std;

int main()
{
const char *x = "hello"; // allowed
char y[] = "hello"; // allowed
string z = "hello"; // allowed
char *w = "hello" ; // Incorrect, but still g++ allows this.
}

The RHS of all these expressions is same. My questions are

i) In which of these cases a type conversion is involved during the
initialization?
ii) When I just say "hello" as a literal does it represent any
particular type?


IIRC, the standard requires that a string literal is a const array of
char. However, some older (pre-standard) versions of C did not
support the const keyword, so the type of a string literal is simple
array of char. I'm not sure offhand if draft versions of the C++
standard (before it was ratified) ever required that a string literal
be a const array, but it is probable some older compilers treated a
string literal as a non-const array of char for reasons of backward
compatibility to C.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Tony Delroy
Guest





PostPosted: Fri Oct 21, 2005 4:28 pm    Post subject: Re: String literals in C++ Reply with quote

Quote:
const char *x = "hello";

results in memory allocation for both a six-character string literal,
and a pointer "x" that you're unlikely to need (it could be moved along
the string, but unless you plan to do so exactly once, or there are
quirks to your string layout that you plan to exploit, you'll need a
second variable to assist with moving towards the start of the string,
so you might as well declare a named string literal and a pointer to
it, rather than two pointers and an unnamed string literal).

i.e. const char y[] = "hello";
const char* p = y;

Quote:
char y[] = "hello";

better to say const char where possible and sufficient, or you may end
up with run-time copying from read-only memory to read-write memory.

Quote:
string z = "hello";

Assuming std::string, this is equivalent to string("hello"), and quite
different from the character array approaches in that at run-time it
will dynamically allocate memory from the heap and then copy the
characters from the string literal.

Quote:
char *w = "hello" ;

As mentioned by others, this is allowed for backwards compatibility
with C. Consider all the C headers declaring things like:

void fn(char* p_string);

It would be very painful to have to type:

fn(const_cast<const char*>("hello"));

in order to call them.

Tony


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Dietmar Kuehl
Guest





PostPosted: Fri Oct 21, 2005 7:34 pm    Post subject: Re: String literals in C++ Reply with quote

Neelesh wrote:

Quote:
I have a confusion regarding what is the exact datatype of a "string
literal" in C++.


This is something which is not really often explained in C++ text
books. However, it is rather simple: the type is 'char const[n]'
where 'n' is the number of characters in the string literal,
including the terminating zero character. That is, "foo" has the
type 'char const[4]'.


Quote:
const char *x = "hello"; // allowed


This takes advantage of the normal decaying of array types to
pointer types.


Quote:
char y[] = "hello"; // allowed


This is entirely unrelated to the type of the string literal: the
above line involves to character array, namely the string literal
"hello" and the character array 'y' whose size is determined from
the string literal and which is initialized by copying the
literal's elements.


Quote:
string z = "hello"; // allowed


This is similar to the previous example in that it is entirely
unrelated to the type of the string literal: a variable is
initialized using a conversion operator taking a pointer to the
string literal which is obtained as the result from decaying the
array into an array.


Quote:
char *w = "hello" ; // Incorrect, but still g++ allows this.


All conforming compilers shall allow this although it breaks
const correctness. This is a special exception applying to string
literals which was added to avoid invalidating legacy code: it
was considered too common that string literals are assigned to
'char*' variables or passed to functions taking this type as
argument. New code should never do anything like this.


Quote:
i) In which of these cases a type conversion is involved during the
initialization?


In all of these cases. In fact, you cannot initialize a variable
with a string literal without a conversion! The closes you can
get is initializing a reference with string literal but even this
has a different type than the string literal itself: it is a
reference to string literal rather than the string literal itself:

char const (&ref)[6] = "hello";


Quote:
ii) When I just say "hello" as a literal does it represent any
particular type?


It has a particular type: 'char const[6]'.
--
<mailto:dietmar_kuehl (AT) yahoo (DOT) com> <http://www.dietmar-kuehl.de/>
<http://www.eai-systems.com> - Efficient Artificial Intelligence

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
kanze
Guest





PostPosted: Sat Oct 22, 2005 1:49 pm    Post subject: Re: String literals in C++ Reply with quote

Alf P. Steinbach wrote:
Quote:
* Neelesh:
I have a confusion regarding what is the exact datatype of a "string
literal" in C++.

array of constant char, or in C++'ish, 'char const [N]'.

The following code makes the point clear -

#include using namespace std;

int main()
{
const char *x = "hello"; // allowed
char y[] = "hello"; // allowed
string z = "hello"; // allowed
char *w = "hello" ; // Incorrect, but still g++ allows this.

Actually the last _is_ formally correct, but only due to a C
compatibility feature; it's not something one should do except
for interfacing to legacy C code.

}

The RHS of all these expressions is same. My questions are

i) In which of these cases a type conversion is involved
during the initialization?

All of them. Unless the second one doesn't count as a
"conversion". There are so many special meanings of words,
and somebody else can look that up!

The second doesn't count as a conversion, because conversions
occur in expressions, and there is no expression:-). Seriously,
from a standards point of view, the type of the token may be
string literal, but the string doesn't work like a string
literal otherwise. In particular, it *doesn't* have a type char
const[]; in fact, it doesn't have a type at all. It's just a
special way of writing:
{ 'h', 'e', 'l', 'l', 'o', '' }.
(Sort of, anyway. The important point is that there isn't a
char const[] which you can access. Typically, at least when
initializing static char[], there isn't a char const[] at all.)

Quote:
ii) When I just say "hello" as a literal does it represent
any particular type?

'char const [6]', with 6 instead of 5 because of the
terminating zero.

Except when it is used to initialize a character array.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Alf P. Steinbach
Guest





PostPosted: Sat Oct 22, 2005 6:09 pm    Post subject: Re: String literals in C++ Reply with quote

* kanze:
Quote:
* Alf P. Steinbach:
* Neelesh:

i) In which of these cases a type conversion is involved
during the initialization?

All of them. Unless the second one doesn't count as a
"conversion". There are so many special meanings of words,
and somebody else can look that up!

The second doesn't count as a conversion, because conversions
occur in expressions, and there is no expression:-)

Thanks, but now I note that Alf has said he's somewhat agnostic on the issue,
baddin ingles languge as he is, Victor has kept silent but with an implied no,
James has said clearly no, and Dietmar has said clearly yes.

This can get interesting. ;-)

Watch the next episode of ... C O N V E R S I O N!

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
James Kanze
Guest





PostPosted: Mon Oct 24, 2005 12:14 am    Post subject: Re: String literals in C++ Reply with quote

Alf P. Steinbach wrote:
Quote:
* kanze:

* Alf P. Steinbach:

* Neelesh:

i) In which of these cases a type conversion is involved
during the initialization?

All of them. Unless the second one doesn't count as a
"conversion". There are so many special meanings of words,
and somebody else can look that up!

The second doesn't count as a conversion, because conversions
occur in expressions, and there is no expression:-)

Thanks, but now I note that Alf has said he's somewhat
agnostic on the issue,

I noted that too. I just like arguing about nits:-). In the
end, of course, what counts is what the thing does, not what you
call it, and I think we all agree as to what it does.

Quote:
baddin ingles languge as he is, Victor has kept silent but
with an implied no, James has said clearly no, and Dietmar has
said clearly yes.

I thought that Dietmar also said no. He described what happens
here without using the word "conversion".

Quote:
This can get interesting. ;-)

Watch the next episode of ... C O N V E R S I O N!

Glad to see that your sense of humor is keeping things in
perspective.

--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Alf P. Steinbach
Guest





PostPosted: Mon Oct 24, 2005 9:00 am    Post subject: Re: String literals in C++ Reply with quote

* James Kanze:
Quote:

I thought that Dietmar also said no. He described what happens
here without using the word "conversion".

No, he wrote "In all of these cases.".


Quote:
This can get interesting. ;-)

Watch the next episode of ... C O N V E R S I O N!

And here we go... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.