Sujet : Re: Two questions on arrays with size defined by variables
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 10. Feb 2025, 01:46:15
Autres entêtes
Organisation : None to speak of
Message-ID : <8734gmr5ig.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6
User-Agent : Gnus/5.13 (Gnus v5.13)
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.comvoid Void(void) { Void(); } /* The recursive call of the void */