Looking for a dot

  • I'll probably get raked over the coals for even suggesting that there is a problem here, but this snippet that I ran across while trying to find the cause of a core dump irritates me, and did in fact elicit a WTF. The crash was NOT caused by this code:

            // find the first '.'
            char tok[2];
    #if defined (WIN32) && !defined (WINCE)
            sprintf_s(tok, sizeof(tok), "%c", '.');
            sprintf(tok, "%c", '.');
            char* c;
            c = strstr(pCard->foo, tok);

    Why not the following?

            char* c = strstr(p->foo, ".");

    I know it's not tragic, not a big deal, but I have a hard enough time figuring stuff out without wading thru clutter and crap.

  • Ugh. Even if the second parameter to strstr() wasn't const (which is what I'm assuming the author thought,) why didn't they just

    static char tok[]] = ".";

  • Perhaps this is a (failed?) attempt at localization? I know I've used something like:
    Dim sepCheck As String = Mid(Format(0.0#, "0.0"), 2, 1)
    to get a (.|,) as defined by the numeric format settings on the customers machine.

  • But hey, atleast it is portable!

  • @rstinejr said:

    Why not the following?

            char* c = strstr(p->foo, ".");
    Or just strchr(p->foo, '.')?

  • The real WTF is... that this is C++ code, not C!

    Non-MS compilers like mingw32 do not warn on using the ANSI C functions instead of the proprietary extensions (that years later got pushed into C11).

    But MS in C mode does not allow the char* declaration at the end, but only supports declarations at start of blocks (C99 has been totally ignored by MS).

    So I conclude this is code compiled by MSVC in C++ mode. And ANY C++ string class has better ways to handle searching for a character than using char*... even std::string! C++ code should never have to manipulate char* string by lowlevel hackery.

  • Discourse touched me in a no-no place

    @OperatorBastardusInfernalis said:

    I advise not using that word too much. Not here.

  • @OperatorBastardusInfernalis said:

    The real WTF is... that this is C++ code, not C!
    Really? Looks more like C to me. I'd be interested in hearing your reasoning that it's C++; there's not a Paamayim Nekudotayim in sight for example.

Log in to reply

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.