473,399 Members | 3,106 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,399 software developers and data experts.

string constant

hello everybody,

consider the following statement,
char *s = "someString";

Does the above statement cause "someString" to be alloted a constant
memory space. What I mean is can't we manipulate "someString" using
statements like

s[0] = 'a';

Oct 20 '06 #1
6 1620
chandanlinster <ch************@gmail.comwrote:
consider the following statement,
char *s = "someString";
Does the above statement cause "someString" to be alloted a constant
memory space.
It might; implementations are allowed to make this choice for
themselves.
What I mean is can't we manipulate "someString" using
statements like
s[0] = 'a';
No. Not portably, at least, although some implementations offer the
ability to modify string literals as an extension.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Oct 20 '06 #2
"chandanlinster" <ch************@gmail.comwrites:
consider the following statement,
char *s = "someString";

Does the above statement cause "someString" to be alloted a constant
memory space. What I mean is can't we manipulate "someString" using
statements like

s[0] = 'a';
No, attempting to modify a string literal invokes undefined behavior.

(For historical reasons, string literals are not treated as "const";
attempting to modify one invokes UB because the standard says so
explicitly.)

Some implementations may allow you to do this. Don't.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 20 '06 #3
Keith Thompson wrote:
"chandanlinster" <ch************@gmail.comwrites:
>>consider the following statement,
char *s = "someString";

Does the above statement cause "someString" to be alloted a constant
memory space. What I mean is can't we manipulate "someString" using
statements like

s[0] = 'a';


No, attempting to modify a string literal invokes undefined behavior.

(For historical reasons, string literals are not treated as "const";
attempting to modify one invokes UB because the standard says so
explicitly.)
But it's still worth declaring them as const.

--
Ian Collins.
Oct 20 '06 #4
Ian Collins <ia******@hotmail.comwrites:
Keith Thompson wrote:
>"chandanlinster" <ch************@gmail.comwrites:
>>>consider the following statement,
char *s = "someString";

Does the above statement cause "someString" to be alloted a constant
memory space. What I mean is can't we manipulate "someString" using
statements like

s[0] = 'a';


No, attempting to modify a string literal invokes undefined behavior.

(For historical reasons, string literals are not treated as "const";
attempting to modify one invokes UB because the standard says so
explicitly.)
But it's still worth declaring them as const.
Yes.

More pedantically, if you have an array whose initializer is a string
literal, it's a good idea to declare the array as const.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 20 '06 #5
Keith Thompson wrote:
Ian Collins <ia******@hotmail.comwrites:
>>Keith Thompson wrote:
>>>"chandanlinster" <ch************@gmail.comwrites:
consider the following statement,
char *s = "someString";

Does the above statement cause "someString" to be alloted a constant
memory space. What I mean is can't we manipulate "someString" using
statements like

s[0] = 'a';
No, attempting to modify a string literal invokes undefined behavior.

(For historical reasons, string literals are not treated as "const";
attempting to modify one invokes UB because the standard says so
explicitly.)

But it's still worth declaring them as const.


Yes.

More pedantically, if you have an array whose initializer is a string
literal, it's a good idea to declare the array as const.
Wouldn't that depend on the purpose of the array?

/* const ??? */ char devname[] = "/dev/pty?";
char *p = strchr(devname, '?');
char *q;
FILE *stream;
for (q = "abc"; *q != '\0'; ++q) {
*p = *q;
stream = fopen(devname, "r+");
if (stream != NULL)
return stream;
}
return NULL;

In my experience, an explicit array is initialized with a
string literal only when one *does* want to modify it; if one
does not, one simply uses the literal. Do you write

const char message[] = "Hello, world!";
puts (message);

or simply

puts ("Hello, world!");

?

--
Eric Sosman
es*****@acm-dot-org.invalid
Oct 21 '06 #6
Eric Sosman <es*****@acm-dot-org.invalidwrites:
Keith Thompson wrote:
[...]
>More pedantically, if you have an array whose initializer is a string
literal, it's a good idea to declare the array as const.

Wouldn't that depend on the purpose of the array?
[snip]

Yes, sorry, I goofed.

If you have a *pointer* whose initializer is a string literal, you
should declare it as const:

const char *p = "hello";

If you have an *array*, declare it as const or not depending on how
you want to use it; the initializer is irrelevant:

char arr[] = "hello";

Here, the string literal is (logically) *copied* to the array. In the
first case, the pointer actually points to the string literal itself.
More precisely, the string literal causes an anonymous array of static
storage duration, just large enough to hold the sequence of
characters, to be created. The pointer points to this array. Any
attempt to modify the array invokes UB.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 21 '06 #7

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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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...
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.