Alright, how does one compare an element of argv with a literal string?

This is a discussion on Alright, how does one compare an element of argv with a literal string? within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Neo1 As mentioned earlier, an array of char's is actually a pointer. Actually, an array of chars ...

  1. #46
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by Neo1
    As mentioned earlier, an array of char's is actually a pointer.
    Actually, an array of chars is an array of chars, not a pointer. However, in most contexts, an array is implicitly converted to a pointer to its first element.

    Quote Originally Posted by Neo1
    The c_string() function in std::string returns a const pointer to an array of chars
    More accurately, the c_str() member function of std::string returns a pointer to a const char, and this const char is the first in a null terminated array of char corresponding to the contents of the std::string.

    Quote Originally Posted by Neo1
    What you WANT to do is this:
    Code:
    #include <cstring>
    ...
    if(std::strcmp(myStdString.c_str(), "--help") )
    This will compare the CONTENTS of the strings, not the memory address, and i think this is what you really want.
    A catch here is that this compares the contents to check if they are not equal. Unless you have some special reason to use the c_str() member function here, it would really be simpler to do without it, e.g.,
    Code:
    if (myStdString == "--help")
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #47
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by skumarisation View Post
    Yeah this you can do by using"operator overloading" for which you need to write a function
    Yeah, I know about operator loading, but I was hoping that the STL would already support using "==" or "!=" with two c-style strings. I don't know why the developers of the C++ STL haven't added it.

  3. #48
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by Programmer_P
    I don't know why the developers of the C++ STL haven't added it.
    They cannot, as one cannot overload on the built-in and pointer types without involving a class type. But a simple solution exists: create a std::string from one of the C-style strings.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #49
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    A catch here is that this compares the contents to check if they are not equal. Unless you have some special reason to use the c_str() member function here, it would really be simpler to do without it, e.g.,
    Code:
    if (myStdString == "--help")
    Yeah, but the problem is "--help" is a c-style string, and "myStdString" is a std::string.
    And the std::string type doesn't have a '==' operator.

  5. #50
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    std::string DOES have an == operator.
    The above code is perfectly legal.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #51
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question

    Quote Originally Posted by laserlight View Post
    They cannot, as one cannot overload on the built-in and pointer types without involving a class type.
    Yeah...so why not involve a class type??

  7. #52
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by Programmer_P
    And the std::string type doesn't have a '==' operator.
    I see. Compile this program and tell us the compile error. If there is no compile error, run the program and tell us the output:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string myStdString = "--help";
        if (myStdString == "--help")
        {
            std::cout << "Programmer_P";
        }
        else
        {
            std::cout << "laserlight";
        }
        std::cout << " is wrong." << std::endl;
    }
    Quote Originally Posted by Programmer_P
    Yeah...so why not involve a class type??
    That is precisely what I am telling you to do: involve a class type by constructing a std::string from one of the null terminated C-style strings.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #53
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    std::string DOES have an == operator.
    The above code is perfectly legal.
    string - C++ Reference

    I don't see any '==' operator...

  9. #54
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    I see. Compile this program and tell us the compile error. If there is no compile error, run the program and tell us the output:
    Code:
    #include <iostream>
    #include <string>
    
    int main()
    {
        std::string myStdString = "--help";
        if (myStdString == "--help")
        {
            std::cout << "Programmer_P";
        }
        else
        {
            std::cout << "laserlight";
        }
        std::cout << " is wrong." << std::endl;
    }
    Ok, fine then...I'm wrong.
    I'll blame it on the writers of the string documentation reference article I just linked to at cplusplus.com.

  10. #55
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    That is precisely what I am telling you to do: involve a class type by constructing a std::string from one of the null terminated C-style strings.
    I meant, why didn't the C++ STL developers involve a class type so they could overload the '==' operator?

  11. #56
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by Programmer_P
    I'll blame it on the writers of the string documentation reference article I just linked to at cplusplus.com.
    Unfortunately for you, they did include those operators: comparison operators

    Quote Originally Posted by Programmer_P
    I meant, why didn't the C++ STL developers involve a class type so they could overload the '==' operator?
    What you want to do is overload operator== for pointer types. If you involve a class type, then you are no longer doing what you set out to do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #57
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Quote Originally Posted by Programmer_P View Post
    string - C++ Reference

    I don't see any '==' operator...
    It may be hard to find a complete reference, but they exist, and so does operator==.

    Quote Originally Posted by the link
    template <class charT, class traits, class Alloc>
    bool operator==(const basic_string<charT, traits, Alloc>& s1,
    const basic_string<charT, traits, Alloc>& s2)

    Container String equality. A global function, not a member function.

    template <class charT, class traits, class Alloc>
    bool operator==(const charT* s1,
    const basic_string<charT, traits, Alloc>& s2)

    basic_string String equality. A global function, not a member function.

    template <class charT, class traits, class Alloc>
    bool operator==(const basic_string<charT, traits, Alloc>& s1,
    const charT* s2)

    basic_string String equality. A global function, not a member function.
    Most references must not cover global functions introduced by the class.

  13. #58
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    Unfortunately for you, they did include those operators: comparison operators
    Ok, but why didn't they put a link to that article on the main C++ string article, which I linked to?

  14. #59
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,648
    Quote Originally Posted by Programmer_P
    Ok, but why didn't they put a link to that article on the main C++ string article, which I linked to?
    They did, but I suppose you can fault them for not making it very prominent, heheh.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #60
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    They did, but I suppose you can fault them for not making it very prominent, heheh.
    Yeh, its on the sidebar, but I meant why didn't they put it on the main part of the page.
    Anyway, ok, I stand corrected...................

Page 4 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Pointer dangling or not
    By vaibhav in forum C++ Programming
    Replies: 16
    Last Post: 08-05-2006, 06:39 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. String literal
    By subdene in forum C++ Programming
    Replies: 5
    Last Post: 11-05-2002, 01:10 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21