🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c
Article #3910

Re: Placement of string literals

#3910
From: Clive D.W. Feather
Date: Sun, 03 Sep 1995 16:23
38 lines
1859 bytes
In article <42c4uu$9e5@sparcserver.lrz-muenchen.de>,
Kurt Watzka <ua302aa@sun2.lrz-muenchen.de> wrote:
> Regarding the placement of string literals, the following question
> came up in de.comp.lang.c:
> Obviously, there are compilers that handle
>    char *foo = "Hello world";
> different form
>    const char *bar = "Hello world";
> as far as the placement of the string literal is concerned.

They are allowed to do so.

> String   
> literals are arrays of "const char". This implies that they can
> be placed in read only memory.

No, they are arrays of char which must not be modified. This implies
that they can be placed in read only memory.

> Does the definition of "foo" somehow
> prevent this placement?

No.

> In other words, what is wrong with the statement: "String literals
> are arrays of const char, and hence may be placed in a read only 
> region of storage, even if they are used to initialize a non const
> char *"?

What is wrong is that the implicit assignment of a const char * to a
char * is forbidden by the Standard. It was felt easier to make string
literals unmodifiable than to insert several special cases just to make
them const char arrays.

-- 
Clive D.W. Feather                       | If you lie to the compiler,
clive@demon.net        (work, preferred) | it will get its revenge.
clive@stdc.demon.co.uk (home)            |   - Henry Spencer
Technical Details
Message-ID: <DEC87J.G11@stdc.demon.co.uk>
Path: arc1-ca.pugleaf.net!stdc.demon.co.uk!clive
References: <42c4uu$9e5@sparcserver.lrz-muenchen.de>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!stdc.demon.co.uk!clive
From: clive@stdc.demon.co.uk (Clive D.W. Feather)
Newsgroups: comp.std.c
Subject: Re: Placement of string literals
Date: Sun, 3 Sep 1995 16:23:40 GMT
Organization: Demon Internet Limited (personal account)
Message-ID: <DEC87J.G11@stdc.demon.co.uk>
References: <42c4uu$9e5@sparcserver.lrz-muenchen.de>
Reply-To: clive@demon.net
Keywords: string literals, const
X-NNTP-Posting-Host: stdc.demon.co.uk