Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: does char *str="abcd"; alloc addressable memory? Date: Mon, 06 Jul 2020 12:20:07 -0700 Organization: None to speak of Lines: 33 Message-ID: <87fta4bgd4.fsf@nosuchdomain.example.com> References: <372e8d3b-52d6-4114-b893-6512633f5c9co@googlegroups.com> <87d058v5m5.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="40782449c3944e27d96f0e3a01ea599f"; logging-data="23705"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18nTIQtVoVszybJ1j16f59N" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) Cancel-Lock: sha1:lewHLBpb2COyFsBsJSflRLmqm3E= sha1:4Px30h4OXeN/Q277MsRBU1rp5z0= Xref: csiph.com comp.lang.c:153085 Ben Bacarisse writes: > Malcolm McLean writes: >> On Monday, 6 July 2020 12:57:06 UTC+1, Bonita Montero wrote: >>> The compiler should give you an error for casting a string-literal to >>> a pointer to non-const characters. If cou cast it explicitly you may >>> modify the caracters and that usually works, but it's not guaranteeed. >>> Most platforms will make a writable copy of a read-only mapped page. >>> >> It doesn't warn because const was a late addition to C, and string literals >> were originally char *s. > > They were, and still are, expressions of type "array of char". Such > expressions are implicitly converted to char * in most contexts. Indeed, the 1975 C Reference Manual (3 years before K&R1, 14 years before the first C standard) says: A string is a sequence of characters surrounded by double quotes ``"''. A string has the type array-of-characters (see below) and refers to an area of storage initialized with the given characters. (It uses the term "string" where modern C uses "string literal".) > I point this out because your reference to what they were originally > might lead someone to think that there has been a change in the language > in this regard. There hasn't been. There was, however, what might be > seen as a missed opportunity when const was added to the language in not > making string laterals have type "array of const char". -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com Working, but not speaking, for Philips Healthcare void Void(void) { Void(); } /* The recursive call of the void */