osdir.com


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

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


On Thu, Oct 12, 2017 at 6:22 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
> Grant Edwards <grant.b.edwards at gmail.com>:
>
>> I like [const qualifiers] in C because it allows the linker to place
>> them in ROM with the code. It also _sometimes_ provides useful
>> diagnostics when you pass a pointer to something which shouldn't be
>> modified to something that is going to try to modify it.
>
> Unfortunately, "const" is so tacky in practice that the language and the
> standard libraries have rendered it useless.
>
> One example is the surprising fact that string literals in C are "char
> *" and not "const char *". Additionally, you can launder any constant
> string into a nonconstant string with strstr(3):
>
>     const char *cs = "hello";
>     char *s = strstr(cs, "");
>     s[0] = 'y';

Well hey, if you want that, you can just cast the pointer. C is not
about preventing you from doing things. The 'const' keyword is a
useful protection (and one that would be more useful if string
literals were const), but it's not meant to be a straitjacket.

ChrisA