473,394 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

String constant

Hi, I need to define some strings in a header file, they are to be
const
Whats the best to choose from below;

const std::string s = "Hello";
const char* s = "Hello";
char* s = "Hello";

Thanks
Jun 27 '08 #1
10 1917
tech wrote:
Hi, I need to define some strings in a header file, they are to be
const
Whats the best to choose from below;

const std::string s = "Hello";
const char* s = "Hello";
char* s = "Hello";

Thanks
I am doing one of two:
const std::string s1 = "Hello";
const char* const s2 = "Hello";
Jun 27 '08 #2
On Jun 6, 4:02 pm, anon <a...@no.nowrote:
tech wrote:
Hi, I need to define some strings in a header file, they are
to be const Whats the best to choose from below;
const std::string s = "Hello";
const char* s = "Hello";
char* s = "Hello";
I am doing one of two:
const std::string s1 = "Hello";
const char* const s2 = "Hello";
Why the pointer?

--
James Kanze (GABI Software) email:ja*********@gmail.com
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

Jun 27 '08 #3
anon wrote:
tech wrote:
>Hi, I need to define some strings in a header file, they are to be
const
Whats the best to choose from below;

const std::string s = "Hello";
const char* s = "Hello";
char* s = "Hello";

Thanks

I am doing one of two:
const std::string s1 = "Hello";
const char* const s2 = "Hello";
Right, don't forget both the const's if you use char *
You want a constant pointer to a constant value.

Depending on your typical use, I would suggest const std::string, since
string adds so much utility.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Jun 27 '08 #4
tech wrote:
Hi, I need to define some strings in a header file, they are to be
const
Whats the best to choose from below;

const std::string s = "Hello";
This is the only version here than can go in a header.
const char* s = "Hello";
This has to be

const char* const s = "Hello";

Which you use depends on how you use them. If you wish to add to one
later (say it's a root file path), use std::string. If they are
discrete tokens, const char* const can be used. But do bear in mind a
std::string will be constructed each tome you pass one to a function
with a const std::string reference parameter.
--
Ian Collins.
Jun 27 '08 #5
James Kanze wrote:
On Jun 7, 11:27 pm, Ian Collins <ian-n...@hotmail.comwrote:
>tech wrote:
>>const char* s = "Hello";
>This has to be
>const char* const s = "Hello";

Again: what's wrong with:

char cosnt s[] = "Hello" ;
It won't compile?
? Why do you need the extra pointer.
No particular reason, just habit. I'm just used to thinking of a
constant pointer to a string literal. A string literal has to have an
address, so where's the extra pointer?

--
Ian Collins.
Jun 27 '08 #6
Hi!

Ian Collins schrieb:
No particular reason, just habit. I'm just used to thinking of a
constant pointer to a string literal. A string literal has to have an
address, so where's the extra pointer?
The pointer itself also has an address. It's one address more than the
array solution has. The array solution saves sizeof(char*) bytes (or
whatever) in the resulting object file (or it is optimized away).

Frank
Jun 27 '08 #7
Frank Birbacher wrote:
Hi!

Ian Collins schrieb:
>No particular reason, just habit. I'm just used to thinking of a
constant pointer to a string literal. A string literal has to have an
address, so where's the extra pointer?

The pointer itself also has an address. It's one address more than the
array solution has. The array solution saves sizeof(char*) bytes (or
whatever) in the resulting object file (or it is optimized away).
That was my point, it will more then likely be optimised away.

--
Ian Collins.
Jun 27 '08 #8
On Jun 8, 12:35 pm, Ian Collins <ian-n...@hotmail.comwrote:
Frank Birbacher wrote:
Ian Collins schrieb:
No particular reason, just habit. I'm just used to
thinking of a constant pointer to a string literal. A
string literal has to have an address, so where's the extra
pointer?
The pointer itself also has an address. It's one address
more than the array solution has. The array solution saves
sizeof(char*) bytes (or whatever) in the resulting object
file (or it is optimized away).
That was my point, it will more then likely be optimised away.
But the extra pointer reduces the probability that the string
literal itself will be optimized away, since it is actually
"used". (Of course, a good compiler should be able to follow
the link---having suppressed the pointer, the string literal is
no longer used. Usual practice for automatic variables, but I
don't know whether this is frequently done for static variables
or not.

--
James Kanze (GABI Software) email:ja*********@gmail.com
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
Jun 27 '08 #9
James Kanze wrote:
On Jun 6, 4:02 pm, anon <a...@no.nowrote:
>tech wrote:
>>Hi, I need to define some strings in a header file, they are
to be const Whats the best to choose from below;
>>const std::string s = "Hello";
const char* s = "Hello";
char* s = "Hello";
>I am doing one of two:
const std::string s1 = "Hello";
const char* const s2 = "Hello";

Why the pointer?
Why not?
PS Sorry for very slow response :(
Jun 27 '08 #10
James Kanze wrote:
On Jun 6, 4:02 pm, anon <a...@no.nowrote:
>tech wrote:
>>Hi, I need to define some strings in a header file, they are
to be const Whats the best to choose from below;
>>const std::string s = "Hello";
const char* s = "Hello";
char* s = "Hello";
>I am doing one of two:
const std::string s1 = "Hello";
const char* const s2 = "Hello";

Why the pointer?
I just read your other response.
Some functions/methods are expecting const char* as parameter. I know I
could do s1.c_str(), but I could pass directly const char*
I guess just a matter of preferences ;)
Jun 27 '08 #11

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

Similar topics

51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
21
by: M D | last post by:
You know how you assume you know until you find out you don't know. Well, I typed into a function definition "..., new String("")). I know what I want. Everyone reading this knows what I want....
34
by: Chad | last post by:
Given the following code that achieves no useful purpose: #include <string.h> #include <stdio.h> #include <string.h> int manip(char *str) { size_t len = strlen(str)-1; if(len >= 3) {
14
by: nospam | last post by:
From the book "There is an important difference between these definitions: char amessage="now is the time"; char *pmessage ="now is the time"; snip On the other hand, pmessage is a...
20
by: karthikbalaguru | last post by:
Hi, String constant being modifiable in C++ but, not modifiable in C. that is, In C++, the following example output will be "Mplusplus" char *str1 = "Cplusplus"; *str1 = 'M'; In C, the above...
18
by: sinbad | last post by:
hi, why does the following program gives an runtime error ,instead of compilation error. anyone please shed some light. thanks sinbad ------------------------------ int main()
13
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
21
by: Ray Cassick | last post by:
I can't believe why I had not noticed this before and why I never asked it before... When defining a string why is it not required to use the new keyword? Like: Dim a As String = New String ...
1
by: mohanprasadgutta | last post by:
hello everyone, i have to write a program which will take a command line arguement which is nothing but a constant name. in the program i am trying to print out the value corresponding to the...
7
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.