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

how to use std:strcpy();



  • i'm trying to convert to code from:
    https://www.geeksforgeeks.org/convert-string-char-array-cpp/
    to work in a qt creator console app.

    strcpy(char_array, s.c_str());
    

    witch result in:

    main.cpp:16: error: cannot convert ‘QString’ to ‘const char*’
       16 |     std:strcpy(char_array, s);
          |                            ^
          |                            |
          |                            QString
    

    the code:

        QString s;
    
        int number = s.length();
    
        // declaring character array
        char char_array[number + 1];
    
        // copying the contents of the
        // string to char array
        std:strcpy(char_array, s.data_ptr());
    

    error:

    main.cpp:16: error: cannot convert ‘QString::DataPtr’ {aka ‘QTypedArrayData<short unsigned int>*’} to ‘const char*’
       16 |     std:strcpy(char_array, s.data_ptr());
          |                            ~~~~~~~~~~^~
          |                                      |
          |                                      QString::DataPtr {aka QTypedArrayData<short unsigned int>*}
    

    string.h fille

    /* Copy SRC to DEST.  */
    extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
    

    regards.


  • Qt Champions 2019

    You have to convert your QString into a 8-bit encoding first. E.g. utf-8: QString::toUtf8()



  • @Natural_Bugger said in how to use std:strcpy();:

    to work in a qt creator console app

    In addition to what @Christian-Ehrlicher has told you to make this work. It is not obvious that you actully need to do such a thing, why do you think it is necessary "to work in a qt creator console app"? These days they are fewer needs to ever use strcpy().



  • @JonB said in how to use std:strcpy();:

    @Natural_Bugger said in how to use std:strcpy();:

    to work in a qt creator console app

    In addition to what @Christian-Ehrlicher has told you to make this work. It is not obvious that you actully need to do such a thing, why do you think it is necessary "to work in a qt creator console app"? These days they are fewer needs to ever use strcpy().

    well, a lot less work to see code do it jobs.
    otherwise you would be dealing with signals, slots, dragging form elements, etc, etc.

    .... and the code challenges I'm looking at are build in console apps.
    so you would spending a whole lot a time "designing" a GUI and that's not part of the challenge.



  • I think the point is, why do you need to convert a QString to a char array?
    Do you need to use it as a parameter of const char* type?
    If that, you don't need that strcpy thing, you can directly use QString::toUtf8() or QString::toLocal8Bit()



  • @Natural_Bugger
    No, my point was only about do you really have any need for a strcpy()? Chances are, you can do what you want from a QString or a std::string, without needing to go to a char * type? And I didn't see any connection between you saying you need a char * because of something about making it work "in a console app".



  • We are converting old code from wxWidgets to Qt. We are not rewriting everything, but just replace calls with equivalents. What has worked for us in your context is to call s.toUtf8().data():

    std::strcpy(char_array, s.toUtf8().data());
    

    However, in very few circumstances we had some strange behaviour in release mode (i.e. with optimizations). Usually, we are not copying the string explicitely, but just assign it (e.g. to a std::string). Sometimes the result is a garbled mess. This might have to do with the temporary which toUtf8() introduces. A reliable solution to this is the following:

    QByteArray tmp = s.toUtf8();
    std::strcpy(char_array, tmp.data());
    

    Lifetime of the QByteArray is extended. It might not be necessary in your specific case, but we consider it a good pattern to avoid accidental errors.


  • Qt Champions 2019

    @SimonSchroeder said in how to use std:strcpy();:

    This might have to do with the temporary which toUtf8() introduces.

    It not even 'might' - it is ... c++ basics.


Log in to reply