implicit vs. explicit type conversion for string vs. (char *)

Discussion in 'C++' started by dmoos AT esigma-systems DOT de, Jun 26, 2003.

  1. Hopefully somebody could teach me, what coding is correct,
    clean and preferable:

    #include <string>

    int main(int argc, char **argv) {
    std::string str = "lala";
    char *cp = "lala";

    return (cp == str) ? 0 : 1;
    }

    Is this code-snippet correct and ANSI-conform or should i
    convert/copy/assign the content of cp into a string before
    comparing to str?

    All comments, hints and critics are highly appreciated.

    Thanks in advance,

    Darius.
     
    dmoos AT esigma-systems DOT de, Jun 26, 2003
    #1
    1. Advertising

  2. dmoos AT esigma-systems DOT de

    Ron Natalie Guest

    "dmoos AT esigma-systems DOT de" <> wrote in message news:bdegjr$s4be3$...

    > Is this code-snippet correct and ANSI-conform or should i
    > convert/copy/assign the content of cp into a string before
    > comparing to str?


    There are operator== overloads in the standard library that
    take combinations of string and char* and do the right thiing.
     
    Ron Natalie, Jun 26, 2003
    #2
    1. Advertising

  3. dmoos AT esigma-systems DOT de

    Default User Guest

    dmoos AT esigma-systems DOT de wrote:

    > #include <string>
    >
    > int main(int argc, char **argv) {
    > std::string str = "lala";
    > char *cp = "lala";
    >
    > return (cp == str) ? 0 : 1;
    > }
    >
    > Is this code-snippet correct and ANSI-conform or should i
    > convert/copy/assign the content of cp into a string before
    > comparing to str?



    Others have discussed the string comparison. I'll take this opportunity
    to point out that returning anything from main() except for 0,
    EXIT_SUCCESS or EXIT_FAILURE (inclusion of stdlib.h or cstdlib required
    for the latter two) leads to implementation-specific results. For
    maximum portability, don't return anything else (like 1) from main().




    Brian Rodenborn
     
    Default User, Jun 26, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. al
    Replies:
    2
    Views:
    5,650
    Ernst Murnleitner
    Jan 7, 2004
  2. /* frank */

    Implicit and explicit conversion

    /* frank */, Jul 3, 2004, in forum: C Programming
    Replies:
    3
    Views:
    404
    Dan Pop
    Jul 5, 2004
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,153
    Ian Collins
    May 9, 2006
  4. utab
    Replies:
    3
    Views:
    618
    Juha Nieminen
    Jun 23, 2007
  5. moorep312

    implicit char conversion in templates

    moorep312, Nov 11, 2010, in forum: C Programming
    Replies:
    0
    Views:
    303
    moorep312
    Nov 11, 2010
Loading...

Share This Page