Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

void* to QString C++



  • I have following code:

    void check()
    {
        QString stringdat = "Hello Qt";
        foo(&stringdat);
    }
    
    void foo(void* ptr)
    {
        QString* data = (QString*) ptr; 
    
         if(data != NULL)
        {
             qDebug() << "Data is " << data; 
        }
    
    }
    

    However the challenge is :

    QString* data = (QString*) ptr;
    

    How do it achieve the void* to QString conversion ?



  • @derric said in void* to QString C++:

    QString* data = (QString*) ptr; 
    qDebug() << "Data is " << data; 
    

    As @eyllanesc has said: since data is a QString *, if you output just that you get a pointer value, like 0x65fe9c. If you want to see what it points to, you need qDebug() << "Data is " << *data;, which dereferences the pointer and will print the QString, which is Hello Qt here.

    There is nothing special in this context because you chose to pass the (address of the) original QString as a void * here.

    As I'm sure you are aware, if you use that C-style cast (QString*) ptr and the ptr passed in does not point to a QString, disaster will occur when you go *data! :)


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You need to dereference the pointer.



  • void pointers are highly discouraged in C++...just sayin. Look at the plethora of "safe coding" standards out there and you'll see what I mean. I'd suggest either using the type system or making templates and avoiding void* altogether.

    Your example use case is a prime example of why it's bad ju-ju. What happens when the internal foo() QString tries to reference a pointer that is NOT a conforming QString?



  • @Kent-Dorfman I have posted the above for a reason and i needed to know how? i'm expecting answers not suggestions or questions back.

    If you could put the question about what happens when the internal QString tries to reference a pointer thats is not QString at https://forum.qt.io/, i'll be happy to answer your question.

    Here i'm not looking forward for why i should not be using void pointers



  • @SGaist Thank you. But my point is i have de referenced it at function call as below:

    foo(&stringdat);
    

    The output i get is as follows :

    Data is  0x65fe9c
    

    However i was expecting

    Data is Hello Qt
    

    Please help me out here. I'm new



  • @derric use qDebug() << "Data is " << *data;



  • @derric said in void* to QString C++:

    QString* data = (QString*) ptr; 
    qDebug() << "Data is " << data; 
    

    As @eyllanesc has said: since data is a QString *, if you output just that you get a pointer value, like 0x65fe9c. If you want to see what it points to, you need qDebug() << "Data is " << *data;, which dereferences the pointer and will print the QString, which is Hello Qt here.

    There is nothing special in this context because you chose to pass the (address of the) original QString as a void * here.

    As I'm sure you are aware, if you use that C-style cast (QString*) ptr and the ptr passed in does not point to a QString, disaster will occur when you go *data! :)



  • @eyllanesc Thanks a ton



  • @JonB Thank you for the explanation. that really helped me realize the problem in detail


Log in to reply