Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #6145

Making the element type of string literals const

Path csiph.com!weretis.net!feeder6.news.weretis.net!feeder5.news.weretis.net!news.solani.org!.POSTED!not-for-mail
From Philipp Klaus Krause <pkk@spth.de>
Newsgroups comp.std.c
Subject Making the element type of string literals const
Date Fri, 14 Aug 2020 11:34:53 +0200
Organization solani.org
Message-ID <rh5lrt$m1k$1@solani.org> (permalink)
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
Injection-Date Fri, 14 Aug 2020 09:34:53 -0000 (UTC)
Injection-Info solani.org; logging-data="22580"; mail-complaints-to="abuse@news.solani.org"
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.11.0
X-Mozilla-News-Host snews://news.solani.org:563
Cancel-Lock sha1:fdpoaFrUVPjPW/J7F/XXsyEe6+s=
Autocrypt addr=pkk@spth.de; keydata= mQGiBD+wIQ0RBAD5IkcgQQIAPO93xBBvviI0htuoaeb++GqG51BImx+rnWIS+1z+r3y5jQU5 IRYS8eDC3eR5PFtde/f7/3zxaJIB0bgXB1eMPot5CaN7mpUySVNh9bzH/UP7rVH0Ut7Bx/CI 7EXxhvYE23F/TAf+gS4flFqQ/GdK+MME+dg8J5nFbwCg92xq6iuAvyz7vSho9qbKHsCD3AkE AMbuLLBvk4mPvs19aRGZvT/pows1uzkeNq8fpmzvryb4UxUbU89qlNzA2Bg8ztVZGX9x5drb C4tFE29ZFAHn2bojx2ADxG85+2as8yfcz6sPG4HMzeTtugzPGCeRAXaSknZHsw6AC6LrtsEU sjEZMElfvu8uU4dthfhXOIDVNRHmA/446GsHRLosDHFuDjHpBQJPJSBnFDyRSCge2Q0gup96 GGhNc7KkYeIB8Dh7dCN3h5RYXflVTspxmgANUa+RFpL4pynsF16rtkfeh0UR8uLdoaGwMChw GyWIdRhX8MLQ8v3WHPCUrB/1f5xhRypQgF0/c8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5MQWOCWqHx0oEPM=
Xref csiph.com comp.std.c:6145

Show key headers only | View raw


C does not allow programs to modify a string literal.

However, for historic reasons this is not reflected in their type.

E.g. this compiled, but has undefined behaviour if ever executed:

void f(void)
{
	"test"[2] = 'a';
}

It would be more consistent is the element type for string literals
would be const-qualified. And coding standards (e.g. MISRA) require the
use of const-qualified types already in e.g.

const char *c = "test"; // OK
vs.
char *c = "test"; // Allowed by C standard, not allowed by MISRA

I don't think this change would break many recent programs. So even if
the type of char string literals cannot be changed, such a change should
at least be possible for string literals of other types.

Is it worth writing a proposal for C2X?
What do you think of such a change?

Philipp

Back to comp.std.c | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Making the element type of string literals const Philipp Klaus Krause <pkk@spth.de> - 2020-08-14 11:34 +0200
  Re: Making the element type of string literals const David Brown <david.brown@hesbynett.no> - 2020-08-14 13:43 +0200
    Re: Making the element type of string literals const Florian Weimer <fw@deneb.enyo.de> - 2020-08-14 23:41 +0200
      Re: Making the element type of string literals const David Brown <david.brown@hesbynett.no> - 2020-08-17 15:23 +0200

csiph-web