🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #1900

Re: strings no longer writable?

#1900
From: Blair P. Houghton
Date: Mon, 29 Oct 1990 23:26
38 lines
1631 bytes
In article <1990Oct29.174410.28498@batcomputer.tn.cornell.edu> lijewski@theory.tn.cornell.edu (Mike Lijewski) writes:
>
>My understanding is that strings are no longer writable.  That is to say,
>the assignment to s[5] in the below program shouldn't be allowed.
>
>main() { char *s = "testing"; s[5] = 'z'; }

Correct.

However, if you had done

	main() { char s[] = "testing"; s[5] = 'z'; }

s would itself be an array and be writable; it is considered
identical to having done

	main() { char s[] = {'t','e','s','t','i','n','g','\0'}; s[5] = 'z'; }

(cf. ANSI X3.159-1989, sec. 3.5.7, p. 75, ll. 24-30)

>Is a conforming implementation required to issue a warning, an error, or
>what?

"...the declaration

    char *p = "abc";

defines p with type `pointer to char' that is initialized
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."
(ibid., ll. 30-34)

Therefore, it's entirely up to your implementor.

				--Blair
				  "Harder to find than to understand..."
Technical Details
Message-ID: <681@inews.intel.com>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!watserv1!watmath!att!att!emory!wuarchive!cs.utexas.edu!sun-barr!apple!olivea!orc!inews!cmdnfs!bhoughto
References: <1990Oct29.174410.28498@batcomputer.tn.cornell.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!watserv1!watmath!att!att!emory!wuarchive!cs.utexas.edu!sun-barr!apple!olivea!orc!inews!cmdnfs!bhoughto
From: bhoughto@cmdnfs.intel.com (Blair P. Houghton)
Newsgroups: comp.std.c
Subject: Re: strings no longer writable?
Message-ID: <681@inews.intel.com>
Date: 29 Oct 90 23:26:43 GMT
References: <1990Oct29.174410.28498@batcomputer.tn.cornell.edu>
Sender: news@inews.intel.com
Organization: Intel Corp, Chandler, AZ
Lines: 38