Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Chris M. Thomasson" 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 12:27:47 -0700 Organization: A noiseless patient Spider Lines: 76 Message-ID: References: <20240801174026.00002cda@yahoo.com> <87zfpvfdk4.fsf@nosuchdomain.example.com> <87v80ig4vt.fsf@nosuchdomain.example.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 02 Aug 2024 21:27:47 +0200 (CEST) Injection-Info: dont-email.me; posting-host="5a662775f5eb6582259b0411a9796340"; logging-data="3124129"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/cW10CDT5gkJi5vg43cTdZRuZJnGwJYhE=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:mJnyeJLPNdl2B0XknrC9g7vjlkc= Content-Language: en-US In-Reply-To: <87v80ig4vt.fsf@nosuchdomain.example.com> Bytes: 3279 On 8/2/2024 11:24 AM, Keith Thompson wrote: > Richard Harnden writes: > [...] >> Is there any reason not to always write ... >> >> static const char *s = "hello, world"; >> >> ... ? >> >> You get all the warnings for free that way. > > The "static", if this is at block scope, specifies that the pointer > object, not the array object, has static storage duration. If it's at > file scope it specifies that the name "s" is not visible to other > translation units. Either way, use it if that's what you want, don't > use it if it isn't. > > There's no good reason not to use "const". (If string literal objects > were const, you'd have to use "const" here.) > > If you also want the pointer to be const, you can write: > > const char *const s = "hello, world"; > For some reason I had a sort of a habit wrt const pointers: (experimental code, no ads, raw text...) https://pastebin.com/raw/f52a443b1 ________________________________ /* Interfaces ____________________________________________________________________*/ #include struct object_prv_vtable { int (*fp_destroy) (void* const); }; struct device_prv_vtable { int (*fp_read) (void* const, void*, size_t); int (*fp_write) (void* const, void const*, size_t); }; struct device_vtable { struct object_prv_vtable const object; struct device_prv_vtable const device; }; struct device { struct device_vtable const* vtable; }; #define object_destroy(mp_self) ( \ (mp_self)->vtable->object.fp_destroy((mp_self)) \ ) #define device_read(mp_self, mp_buf, mp_size) ( \ (mp_self)->vtable->device.fp_read((mp_self), (mp_buf), (mp_size)) \ ) #define device_write(mp_self, mp_buf, mp_size) ( \ (mp_self)->vtable->device.fp_write((mp_self), (mp_buf), (mp_size)) \ ) ________________________________ ;^)