• What Is Wrong With This Function?

    From Lawrence D'Oliveiro@21:1/5 to All on Sat Feb 24 23:54:42 2024
    I think it was Dennis Ritchie who pointed out what a fudge this was:

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

    ANSI C introduced the “const” modifier, and wanted to use it for the arguments to strstr. But if the result is a pointer into the
    “haystack” string, then if that is const, then so should the result
    be. But that means it can only be assigned to a variable that also has
    the “const” modifier.

    Really, it should have become two functions, one where both
    “haystack” and the result are const, and one where neither is.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ben Bacarisse@21:1/5 to Lawrence D'Oliveiro on Sun Feb 25 00:49:34 2024
    Lawrence D'Oliveiro <[email protected]d> writes:

    I think it was Dennis Ritchie who pointed out what a fudge this was:

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

    ANSI C introduced the “const” modifier, and wanted to use it for the arguments to strstr. But if the result is a pointer into the
    “haystack” string, then if that is const, then so should the result
    be. But that means it can only be assigned to a variable that also has
    the “const” modifier.

    Really, it should have become two functions, one where both
    “haystack” and the result are const, and one where neither is.

    Now (in C23) it has become a generic function that preserves const
    correctness in its first argument.

    --
    Ben.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Barry Schwarz@21:1/5 to [email protected] on Sat Feb 24 18:23:45 2024
    On Sat, 24 Feb 2024 23:54:42 -0000 (UTC), Lawrence D'Oliveiro
    <[email protected]d> wrote:

    I think it was Dennis Ritchie who pointed out what a fudge this was:

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

    ANSI C introduced the �const� modifier, and wanted to use it for the >arguments to strstr. But if the result is a pointer into the
    �haystack� string, then if that is const, then so should the result
    be. But that means it can only be assigned to a variable that also has
    the �const� modifier.

    Really, it should have become two functions, one where both
    �haystack� and the result are const, and one where neither is.

    It seems to me that the const modifier in this context does not mean
    no one can ever modify the string. It is a guarantee that the
    function will not modify the string. Once the function returns, all
    bets are off.

    While I don't recall having the need myself, I see nothing wrong with
    calling strstr to find a substring and then doing whatever I want to
    the haystack, such as terminating it either before of after the
    substring by inserting a '\0'.

    --
    Remove del for email

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From James Kuyper@21:1/5 to Keith Thompson on Sun Feb 25 00:57:47 2024
    On 2/24/24 22:10, Keith Thompson wrote:
    ...
    C23 makes strstr() a generic function, so the return value has the type
    of the first argument.

    Nice! I see this also applies to memchr, strchr, strpbrk, and strrchr.
    However, I think they missed and opportunity to generalize. Those
    functions are generic only with respect to the type qualifiers. I think
    it would have been even better if they had been made generic over the
    character type, allowing wchar_t, char16_T, and char32_t.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)