Connecting Tech Pros Worldwide Forums | Help | Site Map

string constant

chandanlinster
Guest
 
Posts: n/a
#1: Oct 20 '06
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';


Christopher Benson-Manica
Guest
 
Posts: n/a
#2: Oct 20 '06

re: string constant


chandanlinster <chandanlinster@gmail.comwrote:
Quote:
consider the following statement,
char *s = "someString";
Quote:
Does the above statement cause "someString" to be alloted a constant
memory space.
It might; implementations are allowed to make this choice for
themselves.
Quote:
What I mean is can't we manipulate "someString" using
statements like
Quote:
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.
Keith Thompson
Guest
 
Posts: n/a
#3: Oct 20 '06

re: string constant


"chandanlinster" <chandanlinster@gmail.comwrites:
Quote:
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) kst-u@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.
Ian Collins
Guest
 
Posts: n/a
#4: Oct 20 '06

re: string constant


Keith Thompson wrote:
Quote:
"chandanlinster" <chandanlinster@gmail.comwrites:
>
Quote:
>>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.
Keith Thompson
Guest
 
Posts: n/a
#5: Oct 21 '06

re: string constant


Ian Collins <ian-news@hotmail.comwrites:
Quote:
Keith Thompson wrote:
Quote:
>"chandanlinster" <chandanlinster@gmail.comwrites:
>>
Quote:
>>>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) kst-u@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.
Eric Sosman
Guest
 
Posts: n/a
#6: Oct 21 '06

re: string constant


Keith Thompson wrote:
Quote:
Ian Collins <ian-news@hotmail.comwrites:
>
Quote:
>>Keith Thompson wrote:
>>
Quote:
>>>"chandanlinster" <chandanlinster@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
esosman@acm-dot-org.invalid


Keith Thompson
Guest
 
Posts: n/a
#7: Oct 21 '06

re: string constant


Eric Sosman <esosman@acm-dot-org.invalidwrites:
Quote:
Keith Thompson wrote:
[...]
Quote:
Quote:
>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) kst-u@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.
Closed Thread