Deutsch   English   Français   Italiano  
<ka6ncjp0ca2dvf6v6lbg6faindvgmujtoa@4ax.com>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dave_thompson_2@comcast.net
Newsgroups: comp.lang.c
Subject: Re: No warning at implicit removal of const. Was: relearning C: why does an in-place change to a char* segfault?
Date: Sun, 25 Aug 2024 16:52:15 -0400
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <ka6ncjp0ca2dvf6v6lbg6faindvgmujtoa@4ax.com>
References: <IoGcndcJ1Zm83zb7nZ2dnZfqnPWdnZ2d@brightview.co.uk> <20240801174026.00002cda@yahoo.com> <v8gi7i$29iu1$1@dont-email.me> <slrnvaorkl.34j6.candycanearter07@candydeb.host.invalid> <87zfpvfdk4.fsf@nosuchdomain.example.com> <v8ii17$2q5p1$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 25 Aug 2024 22:52:26 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="09f9e4e3fd1f3f0a9b1d8559f78a3163";
	logging-data="2199678"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19JPUPYrA6ddUpJ4AkGUhkAInLmdmARhPE="
Cancel-Lock: sha1:J8MqOzAQdLkxJShejy5u+fFeka8=
X-Newsreader: Forte Agent 3.3/32.846
Bytes: 1845

On Fri, 2 Aug 2024 13:04:55 +0100, Richard Harnden
<richard.nospam@gmail.invalid> wrote:

[string literals not typed const in C even though writing prohibited]

> Is there any reason not to always write ...
> 
> static const char *s = "hello, world";
> 
> ... ?
> 
> You get all the warnings for free that way.

But sizeof s is 8 or 4 regardless of the string, while sizeof "some
string" is the length of the string plus 1 (for the null terminator).

static const char s[] = "hello, world"; 
// autosized by initializer

would be a better replacement, or in C99+ if at file scope

(const char[]){"hello, world"}