[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Lies in education [was Re: The "loop and a half"]

On 2017-10-12, Neil Cerutti <neilc at norwich.edu> wrote:
> On 2017-10-12, Marko Rauhamaa <marko at pacujo.net> wrote:
>> Bill <BILL_NOSPAM at whoknows.net>:
>>> Marko Rauhamaa wrote:
>>>> One example is the surprising fact that string literals in C
>>>> are "char *" and not "const char *".

Yep, that's the basis for a lot of the problems with 'const' in C.
Unfortunately for historical reasons, a lot of people expect string
literals to be mutable.  That should have been stamped out decades
ago, but it's too late now.

>>> If not, you couldn't pass a string literal to a function
>>> having prototype void f(char *s);

IMO, it should be illegal to pass a string literal to a function with
prototype f(char *s).  You shouldn't try to modify a string literal,
and if f() isn't going to modify the string, it should have been
declared f(const char *s).

>> That *ought* to be prevented. That's the whole point.
> I'm far less experienced in C, but I threw up my hands and stopped
> bothering with const qualifiers in C due to such headaches. When in
> Rome, program without const qualifiers in C.

Using const with strings in C with amateurish libraries is a headache
because _some_people_ will write their declarations so as to require
pointers to mutable strings even when they have no intention of
mutating them.  Those people should be hunted down and slapped with a
herring until they understand the error of their ways.

The standard library is much better about that.

Grant Edwards               grant.b.edwards        Yow! Kids, don't gross me
                                  at               off ... "Adventures with
                              gmail.com            MENTAL HYGIENE" can be
                                                   carried too FAR!