[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, Marko Rauhamaa <marko at pacujo.net> wrote:
> 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.

That's indeed a problem.  Personally, I would just use two prototypes:

  char *strcstr(const char *s, char *s);
  const char *cstrstr(const char *s, const char *s);

Whether you want to invoke some linker-script magic to make them refer
to the same blob of code or not is optional.

Grant Edwards               grant.b.edwards        Yow! I want my nose in
                                  at               lights!