473,796 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1952
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.nowrot e:
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 objektorientier ter Datenverarbeitu ng
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.co mwrote:
>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.co mwrote:
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 objektorientier ter Datenverarbeitu ng
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.nowrot e:
>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

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

Similar topics

51
8299
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 code? many thx!
21
5760
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. However, the 1.0 compiler has no idea what I want. Can anyone share with me the syntax that will actually communicate the concept to the compiler, please?
34
1902
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
1860
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 pointer, initialized to point a string const; the pointer may be modified to point elsewhere, but the
20
691
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 example will produce an error as we are trying to modify a constant. So, str1 is not a string constant in C++.
18
1931
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
21853
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 will concatenate the value of ABC to MYSTR . I need this at compile time.
21
1463
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 You can do it when using the string type with some arguments in the constructor but it look like there is no empty constructor on the String class.
1
2426
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 constant name provided. i am aware of converting a string to a variable name as follows. my $name = "mohan"; my $var = "name"; my $new = $$name; print "your name is : " . $name . "\n"; But here i have to get the constant value corresponding to...
7
4269
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 expression") or Comeau Online ("constant value is not known"). If I replace
0
9527
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,...
0
10453
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10223
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
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
9050
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...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
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
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
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

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.