Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to use std:strcpy();

how to use std:strcpy();

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 2.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Natural_Bugger
    wrote on last edited by Natural_Bugger
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      5
      • N Natural_Bugger

        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.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @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().

        N 1 Reply Last reply
        0
        • JonBJ JonB

          @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().

          N Offline
          N Offline
          Natural_Bugger
          wrote on last edited by
          #4

          @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.

          JonBJ 1 Reply Last reply
          0
          • B Offline
            B Offline
            Bonnie
            wrote on last edited by
            #5

            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()

            1 Reply Last reply
            0
            • N Natural_Bugger

              @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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @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".

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SimonSchroeder
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                2
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @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.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved