string constant

C

chandanlinster

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';
 
C

Christopher Benson-Manica

chandanlinster said:
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.
 
K

Keith Thompson

chandanlinster said:
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.
 
I

Ian Collins

Keith said:
chandanlinster said:
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.
 
K

Keith Thompson

Ian Collins said:
Keith said:
chandanlinster said:
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.
 
E

Eric Sosman

Keith said:
Ian Collins said:
Keith said:
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!");

?
 
K

Keith Thompson

Eric Sosman said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top