Connecting Tech Pros Worldwide Forums | Help | Site Map

Where can I find more about string literals?

WaterWalk
Guest
 
Posts: n/a
#1: Mar 9 '06
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.


pemo
Guest
 
Posts: n/a
#2: Mar 9 '06

re: Where can I find more about string literals?


WaterWalk wrote:[color=blue]
> 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.[/color]

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.


--
==============
Not a pedant
==============


John Tsiombikas (Nuclear / Mindlapse)
Guest
 
Posts: n/a
#3: Mar 10 '06

re: Where can I find more about string literals?


On 2006-03-09, WaterWalk <toolmaster@163.com> wrote:[color=blue]
>
> 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 *"?[/color]

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.

--
John Tsiombikas (Nuclear / Mindlapse)
nuclear@siggraph.org
http://nuclear.demoscene.gr/
Peter Nilsson
Guest
 
Posts: n/a
#4: Mar 10 '06

re: Where can I find more about string literals?



pemo wrote:[color=blue]
> WaterWalk wrote:[color=green]
> > 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.[/color][/color]

But there is a topic on "String literals"!
[color=blue][color=green]
> > Where are they then? Thanks for your help.[/color]
>
> From [ISO/IEC 9899:1999] 6.7.8 Initialization [32]
>
> The contents of the arrays are modifiable. <snip>[/color]

Completely irrelevant.

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

--
Peter

Micah Cowan
Guest
 
Posts: n/a
#5: Mar 10 '06

re: Where can I find more about string literals?


"Peter Nilsson" <airia@acay.com.au> writes:
[color=blue]
> pemo wrote:[color=green]
> > WaterWalk wrote:[color=darkred]
> > > 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.[/color][/color]
>
> But there is a topic on "String literals"!
>[color=green][color=darkred]
> > > Where are they then? Thanks for your help.[/color]
> >
> > From [ISO/IEC 9899:1999] 6.7.8 Initialization [32]
> >
> > The contents of the arrays are modifiable. <snip>[/color]
>
> Completely irrelevant.[/color]

Only the part you didn't snip. The part you snipped was /entirely/
relevant. However, it was also non-normative (example).
[color=blue]
> Try 6.4.5 String literals, p6 "...If the program attempts to modify
> such an array, the
> behavior is undefined."[/color]
Mark McIntyre
Guest
 
Posts: n/a
#6: Mar 12 '06

re: Where can I find more about string literals?


On 9 Mar 2006 03:21:21 -0800, in comp.lang.c , "WaterWalk"
<toolmaster@163.com> wrote:

(of string literals)
[color=blue]
>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.[/color]

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
--
"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Dave Thompson
Guest
 
Posts: n/a
#7: Mar 20 '06

re: Where can I find more about string literals?


On 9 Mar 2006 03:21:21 -0800, "WaterWalk" <toolmaster@163.com> wrote:
[color=blue]
> 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.[/color]

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
lawrence.jones@ugs.com
Guest
 
Posts: n/a
#8: Mar 20 '06

re: Where can I find more about string literals?


Dave Thompson <david.thompson1@worldnet.att.net> wrote:[color=blue]
>
> 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.[/color]

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
Closed Thread