Where can I find more about string literals?

W

WaterWalk

Hi. Today when I was writing some code fragment, I suddently noticed
that string literals are usually not modifiable. But why can it be
assigned to a variable of type "char *"? I searched the comp.lan.c faq,
and found the answer in Q1.32(http://c-faq.com/decl/strlitinit.html).

I want to learn more about this subject, then I checked the references,
among which is "ISO Sec. 6.1.4, Sec. 6.5.7 ". I downloaded n1124(C99
with TC1, TC2 draft). But I can't find such topics as the type of
string literal in those sections. Where are they then? Thanks for your
help.
 
P

pemo

WaterWalk said:
Hi. Today when I was writing some code fragment, I suddently noticed
that string literals are usually not modifiable. But why can it be
assigned to a variable of type "char *"? I searched the comp.lan.c
faq, and found the answer in
Q1.32(http://c-faq.com/decl/strlitinit.html).

I want to learn more about this subject, then I checked the
references, among which is "ISO Sec. 6.1.4, Sec. 6.5.7 ". I
downloaded n1124(C99 with TC1, TC2 draft). But I can't find such
topics as the type of string literal in those sections. Where are
they then? Thanks for your help.

From [ISO/IEC 9899:1999] 6.7.8 Initialization [32]

The contents of the arrays are modifiable. On the other hand, the
declaration

char *p = "abc";

defines p with type "pointer to char" and initializes it to point to an
object with type "array of char" with length 4 whose elements are
initialized with a character string literal. If an attempt is made to use p
to modify the contents of the array, the behavior is undefined.
 
J

John Tsiombikas (Nuclear / Mindlapse)

Hi. Today when I was writing some code fragment, I suddently noticed
that string literals are usually not modifiable. But why can it be
assigned to a variable of type "char *"?

A variable of type "char *" can be modified, no problem.
char *foo = "a string";
foo = "another string";
That is, foo is a pointer and can be reassigned without problems, if
however that pointer happens to point to a string literal (as is the
case here), you cannot use that pointer to modify the memory that those
string litterals occupy.

The practical reason for this is to allow sharing of a string litteral.
 
P

Peter Nilsson

But there is a topic on "String literals"!
Where are they then? Thanks for your help.

From [ISO/IEC 9899:1999] 6.7.8 Initialization [32]

The contents of the arrays are modifiable. <snip>

Completely irrelevant.

Try 6.4.5 String literals, p6 "...If the program attempts to modify
such an array, the
behavior is undefined."
 
M

Micah Cowan

Peter Nilsson said:
But there is a topic on "String literals"!
Where are they then? Thanks for your help.

From [ISO/IEC 9899:1999] 6.7.8 Initialization [32]

The contents of the arrays are modifiable. <snip>

Completely irrelevant.

Only the part you didn't snip. The part you snipped was /entirely/
relevant. However, it was also non-normative (example).
 
M

Mark McIntyre

On 9 Mar 2006 03:21:21 -0800, in comp.lang.c , "WaterWalk"

(of string literals)
I want to learn more about this subject, then I checked the references,
among which is "ISO Sec. 6.1.4, Sec. 6.5.7 ". I downloaded n1124(C99
with TC1, TC2 draft). But I can't find such topics as the type of
string literal in those sections. Where are they then? Thanks for your
help.

Don't know if you ever got answers so:

6.5.4(6) ... If the program attempts to modify such an array, the
behavior is undefined.
Mark McIntyre
 
D

Dave Thompson

Hi. Today when I was writing some code fragment, I suddently noticed
that string literals are usually not modifiable. But why can it be
assigned to a variable of type "char *"? I searched the comp.lan.c faq,
and found the answer in Q1.32(http://c-faq.com/decl/strlitinit.html).

I want to learn more about this subject, then I checked the references,
among which is "ISO Sec. 6.1.4, Sec. 6.5.7 ". I downloaded n1124(C99
with TC1, TC2 draft). But I can't find such topics as the type of
string literal in those sections. Where are they then? Thanks for your
help.

The references in the FAQ were (AFAIK all) to C90. C99 added or
promoted several sections, resulting in following ones being
renumbered. Former 6.1.4 is now 6.4.5 (String literals) and 6.5.7 is
now 6.7.8 (Initialization, particularly of char[n] by "string").

And its references to Rationale were to the ANSI version, C89, whose
numbering (only) differs from C90, for which JTC1 did not release a
rationale. Since at least 1999 WG14 has been making public at least
some draft Rationales (I don't think they ever approved one as final),
which (now) use the ISO and specifically C99 numbering.

- David.Thompson1 at worldnet.att.net
 
L

lawrence.jones

Dave Thompson said:
Since at least 1999 WG14 has been making public at least
some draft Rationales (I don't think they ever approved one as final),
which (now) use the ISO and specifically C99 numbering.

The current Rationale is intended to be a living document that is
updated from time to time, so there's never going to be a final version.
There's a link to the current version on the committee's home page:

<http://www.open-std.org/jtc1/sc22/wg14/>

-Larry Jones

OK, what's the NEXT amendment say? I know it's in here someplace. -- Calvin
 

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

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top