Deutsch English Français Italiano |
<v8ja83$2us0r$2@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: James Kuyper <jameskuyper@alumni.caltech.edu> 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: Fri, 2 Aug 2024 14:58:10 -0400 Organization: A noiseless patient Spider Lines: 23 Message-ID: <v8ja83$2us0r$2@dont-email.me> 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> <87v80ig4vt.fsf@nosuchdomain.example.com> <82cc9501de86336cdef9fe610bce8e75238fb679@i2pn2.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Injection-Date: Fri, 02 Aug 2024 20:58:11 +0200 (CEST) Injection-Info: dont-email.me; posting-host="fc09beaaa05891c9bcdd65835817b51b"; logging-data="3108891"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/snILBBnGIHUPekbwTFbRFiAoaYY4oJZE=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:S/ClRUrA/ZDoyQ60kSx7aqkUyAQ= In-Reply-To: <82cc9501de86336cdef9fe610bce8e75238fb679@i2pn2.org> Content-Language: en-US On 8/2/24 14:42, Richard Damon wrote: > On 8/2/24 2:24 PM, Keith Thompson wrote: >> Richard Harnden <richard.nospam@gmail.invalid> writes: >> [...] >>> Is there any reason not to always write ... >>> >>> static const char *s = "hello, world"; >>> >>> ... ? .... >> There's no good reason not to use "const". (If string literal objects >> were const, you'd have to use "const" here.) .... > The one good reason to not make it const is that if you are passing it > to functions that take (non-const) char* parameters that don't > actually change that parameters contents. Actually, that's not a good reason. If you can't modify the function's interface, you should use a (char*) cast, which will serve to remind future programmers that this is a dangerous function call. You shouldn't make the pointer's own type "char *".