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. [solved] constructing c-string-array (const char * []) from qstringlist
QtWS25 Last Chance

[solved] constructing c-string-array (const char * []) from qstringlist

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 9.0k 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.
  • K Offline
    K Offline
    krrl
    wrote on last edited by
    #1

    @
    int i =0;
    QStringList list;
    list << "uno" << "dos" << "tres";
    int size = list.size();
    const char *c[size];
    foreach(QString s, list)
    {
    c[i] = s.toLocal8Bit().constData();
    i++;
    }

    cout << endl << "print c-string-array" << endl;
    i = 0;
    while( i < size)
        cout << c[i++] << endl;
    

    @

    Returns three times tres. Tried using QString::const_iterator or QString[] to no avail.
    Id be glad if someone could enlighten me on what is going on.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Check the value of the pointers in c, I wouldn't be surprised that it points to the same address (I haven't tested it, no Linux at hand)

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dbzhang800
        wrote on last edited by
        #3

        Please note that, in following line
        @
        c[i] = s.toLocal8Bit().constData();
        @

        you get a wild pointer saved in you c[i]. First, a temp QByteArray was created by toLocal8Bit. Then a pointer was got from the QByteArray. Finially, you destroy the QByteArray just after the assignment.

        1 Reply Last reply
        0
        • K Offline
          K Offline
          krrl
          wrote on last edited by
          #4

          Ah, ok!
          So copying data from temp QByteArray makes it persistent.
          I replaced
          @ c[i] = s.toLocal8Bit().constData(); @

          with
          @ c[i] = new char[s.toLocal8Bit().size()];
          strcpy(c[i],s.toLocal8Bit().constData()); @

          And removed const from c.
          Thanks for bringing me on the right track :)

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dbzhang800
            wrote on last edited by
            #5

            OK, but note that,

            For C-string char*, the data should be '\0'-terminated, which means the memory space needed should be string of length + 1.

            And normally, for a QByteArray, it can contain '\0' char at any place, and may be not terminated with a '\0'.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              krrl
              wrote on last edited by
              #6

              In this example the strings are null-terminated though.
              So in general you mean I should be using something like
              @
              c[i] = new char[s.toLocal8Bit().size()+1];
              strcpy(c[i],s.toLocal8Bit().constData());
              c[strlen(s[i])] = '\0';
              @

              Anyhow thanks again for the hint, ill be '\0'-aware!

              1 Reply Last reply
              0

              • Login

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