Datentyp-Problem bei strcmp()



  • Hi,

    ich bekomme als Rückgabetyp einer Funktion einen Zeiger auf ein xmlChar, der laut typedef ein unsigned char ist. Nun möchte ich diesen Wert mit einer Stringkonstante vergleichen, nur irgendwie bekomme ich das mit den Datentypen nicht so wirklich hin. Vielleicht noch ein bißchen Code:

    xmlChar *node = xmlTextReaderLocalName(reader);
    if (strcmp(node, "string")==0)
    ...
    

    Ich hoffe mir kann jemand helfen und sagen ob und wenn ja wie ich da etwas casten muss.

    mfg chuqa



  • machste so:

    #include <iostream>
    #include <cstring>
    #include <string>
    
    using namespace std;
    
    typedef unsigned char xmlChar;
    
    xmlChar *xmlTextReaderLocalName( int reader )
    {
        static xmlChar foo[ ] = "string";
        return foo;
    }
    
    int main( )
    {
        int reader = 0;
    
        xmlChar *node = xmlTextReaderLocalName( reader );
    
        cout << "\"" << node << "\" ";
    
        if( strcmp( reinterpret_cast< const char* >( node ), "string" ) == 0 ) // <- edited
            cout << "==";
        else
            cout << "!=";
    
        cout << " \"string\"" << endl;
    
        // oder einfacher:
    
        string node1 = reinterpret_cast< char* >( xmlTextReaderLocalName( reader ) );
    
        cout << "\"" << node << "\" ";
    
        if( node1.compare( "string" ) == 0)
            cout << "==";
        else
            cout << "!=";
    
        cout << " \"string\"" << endl;
    }
    

    Greetz, Swordfish

    [edit]
    const_cast nach const xmlChar* aus

    if( strcmp( reinterpret_cast< const char* >( const_cast< const xmlChar* >( node ) ), "string" ) == 0 )
    

    entfernt.
    [/edit]



  • OK, herzlichen Dank.

    mfg chuqa


  • Mod

    reinterpret_cast< const char* >( const_cast< const xmlChar* >( node ) )
    

    der const_cast ist hier überflüssig.



  • Hast recht. ge-edited.

    Greetz, Swordfish


Anmelden zum Antworten