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

Marko Rauhamaa marko at pacujo.net
Thu Oct 12 05:20:03 EDT 2017


Chris Angelico <rosuav at gmail.com>:

> On Thu, Oct 12, 2017 at 6:22 PM, Marko Rauhamaa <marko at pacujo.net> wrote:
>> 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.

Point is, there is no legitimate way to implement the strstr(3)
prototype. Somebody must be lying through their teeth.

The idea of "const" (and other type declaration paraphernalia) is to
prevent accidental bugs at compile time. The noble idea of "const" has
been undermined by its sloppy use by the standard libraries and the
language itself.

BTW, C++ tries to be a bit stricter about "const". It declares two
separate prototypes:

   const char *strstr(const char *, const char *);
   char *strstr(char *, const char *);

<URL: http://www.cplusplus.com/reference/cstring/strstr/>

Also, in C++, string literals are "const char *".


Marko



More information about the Python-list mailing list