Deutsch   English   Français   Italiano  
<8734gmr5ig.fsf@nosuchdomain.example.com>

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

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail
From: Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: Two questions on arrays with size defined by variables
Date: Sun, 09 Feb 2025 16:46:15 -0800
Organization: None to speak of
Lines: 73
Message-ID: <8734gmr5ig.fsf@nosuchdomain.example.com>
References: <vo9mns$gsd7$1@dont-email.me> <vo9nn3$gtph$1@dont-email.me>
	<vo9u0u$i0n8$1@dont-email.me> <20250209123918.0000754f@yahoo.com>
	<voao0d$o71o$1@dont-email.me> <20250209195711.00001bde@yahoo.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Mon, 10 Feb 2025 01:46:15 +0100 (CET)
Injection-Info: dont-email.me; posting-host="f4acd3ba793eeb2facf0b27d62bf5cd0";
	logging-data="945334"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19K9xCQfCr2SpdZSY6mJOpd"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:n8+DkUPi+QZrrjqn04Z0BOTee78=
	sha1:eVkzkOaIb0XHh49CxwaqO2iqj5o=
Bytes: 4288

Michael S <already5chosen@yahoo.com> writes:
> On Sun, 9 Feb 2025 18:18:04 +0100
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>> On 09.02.2025 11:39, Michael S wrote:
>> > On Sun, 9 Feb 2025 10:54:36 +0100
>> > Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>> > [...]
>> > 
>> > There is another problem in your code - it assigns string literal to
>> > non-const char*. It is legal, as far as 'C' Standard is concerned,
>> > but makes very little practical sense, because any attempt to
>> > assign to string literal through resulting pointer is UB. And not
>> > just a theoretical UB, but a real-world UB.  
>> 
>> This comment specifically draw my attention and made me nervous.
>> 
>> You know, I'm rarely programming in plain "C", and while in C++
>> I generally try to program in "const-correct" form 
>
> Which, I suppose, is not easy.
>
>> I never make
>> use of 'const' in "C". - Unless the compiler complains about it,
>> but I don't recall it (ever?) did.
>> 
>> In my test application I actually never assign string literals
>> or strings to any other string object (modulo the buffer that I
>> filled with a 'fgets'). I operate solely with pointers to 'argv'
>> elements and to the 'char buf[]' buffer data.

There's no such thing as a "string object" in C.  See below.

>> Do you see any issue with that?
[...]
> I see no issues.
>
> Generally, due to absence of user-defined polymorphism, C does not have
> the type of ugly surprises with constness that make life of C++
> programmers miserable. Still, behavior of string literals can be
> surprising.
> I would guess that if it was feasible to make a breaking changes, C89
> would define type of string literals as 'const char*' rather than
> 'char*'. But breaking changes were not feasible.

The type of string literals would be const char[N], not const char*.
(C++ did exactly that.)

In C, a *string* is by definition "a contiguous sequence of characters
terminated by and including the first null character".  A *pointer to a
string* is "a pointer to its initial (lowest addressed) character".  A
string is not a data type; it's a data layout.  An array of char may or
may not have a string *as its contents* (or part of its contents).

A string literal "foo" represents an anonymous array object, in this
case of type char[4].  C++ made string literals const, but C did not.
In C, any attempt to modify the contents of the array object
corresponding to a string literal has undefined behavior.

In C, you can legally write:
    char *ptr = "hello";
but then something like `ptr[0] = 'H';` is legal but has undefined
behavior, and likely will not trigger a warning.  The recommended
practice is that any pointer to a string should be defined with "const":
    const char *ptr = "hello";
so that if you later try `ptr[0] = 'H';` it will be rejected.

Sections 6 and 8 of the comp.lang.c FAQ, <https://www.c-faq.com/>,
cover "Arrays and Pointers" and "Characters and Strings",
respectively.

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */