Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. error in print value of const *char in data in Qt?
QtWS25 Last Chance

error in print value of const *char in data in Qt?

Scheduled Pinned Locked Moved Solved C++ Gurus
memcpyconst charqdebugarray
7 Posts 2 Posters 4.8k 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.
  • stackprogramerS Offline
    stackprogramerS Offline
    stackprogramer
    wrote on last edited by
    #1

    Hi, i define a const char * Data and i used memcpy to copy some varrible to it,you can see snippet of code in below:

    int TC=32;                   
    const char *Data[TC*192+32];
    memcpy(Data,&CD,4);
    memcpy(Data+4,&TC,4);
    memcpy(Data+8,&SZE,4);
    memcpy(Data+12,&R_Code,4);
    memcpy(Data+13,&Star,1);
    memcpy(Data+14,&SFI,1);
    

    Now when i want to print data in std::cout or qDebug() in form below link:

        std::cout << data;
    

    https://stackoverflow.com/questions/4614687/how-to-print-data-inside-a-char-pointer-c
    it reterns nothing,
    when i used this method:

    qDebug()<<"value="<<Data[0];
    

    it crashed with this message:

    Press <RETURN> to close this window...
    
    

    how can print all data in this position in Qt
    thank you

    VRoninV 1 Reply Last reply
    0
    • stackprogramerS stackprogramer

      Hi, i define a const char * Data and i used memcpy to copy some varrible to it,you can see snippet of code in below:

      int TC=32;                   
      const char *Data[TC*192+32];
      memcpy(Data,&CD,4);
      memcpy(Data+4,&TC,4);
      memcpy(Data+8,&SZE,4);
      memcpy(Data+12,&R_Code,4);
      memcpy(Data+13,&Star,1);
      memcpy(Data+14,&SFI,1);
      

      Now when i want to print data in std::cout or qDebug() in form below link:

          std::cout << data;
      

      https://stackoverflow.com/questions/4614687/how-to-print-data-inside-a-char-pointer-c
      it reterns nothing,
      when i used this method:

      qDebug()<<"value="<<Data[0];
      

      it crashed with this message:

      Press <RETURN> to close this window...
      
      

      how can print all data in this position in Qt
      thank you

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      @stackprogramer said in error in print value of const *char in data in Qt?:

      int TC=32;
      const char Data[TC192+32];

      This is not valid C++, dynamic memory allocation must be explicit with new (or, in this case, even malloc).
      On top of that, if you declare it const it means you don't want to change it afterwards but here you do.
      and cherry on top, you are declaring an array of dangling const pointers to char which is for sure not what you want to do.
      What type are CD,TC,SZE,R_Code,Star and SFI?

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      4
      • stackprogramerS Offline
        stackprogramerS Offline
        stackprogramer
        wrote on last edited by
        #3

        thanks for reply CD and ... has 4byte length are int type and other int8_t type.

        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          If they are ints I don't understand what you are trying to do.
          std::cout << data; (even assuming data is done correctly) would always print junk.

          P.S.
          This has nothing to do with Qt

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • stackprogramerS Offline
            stackprogramerS Offline
            stackprogramer
            wrote on last edited by
            #5

            Finally i used this declaration:

            char* DataOutRation;
             DataOutRation=new  char[TC*sizeof(TRAJECTORY)+32];
            

            so i conculded by helping @VRonin that i should changed const char * to char *,
            and memcpy:

             memcpy(DataOutRation,&CD,4);
             memcpy(DataOutRation+4,&TC,4);
             memcpy(DataOutRation+8,&SZE,4);
             memcpy(DataOutRation+12,&R_Code,4);
            

            and i delete array:

            delete [] DataOutRation
            

            for udp i change my method i used char *data for udp:

            qint64 pendingDatagramSize() const;
                qint64 readDatagram(char *data, qint64 maxlen, QHostAddress *host = Q_NULLPTR, quint16 *port = Q_NULLPTR);
                qint64 writeDatagram(const char *data, qint64 len, const QHostAddress &host, quint16 port);
                inline qint64 writeDatagram(const QByteArray &datagram, const QHostAddress &host, quint16 port)
                    { return writeDatagram(datagram.constData(), datagram.size(), host, port); }
            
            

            now every thing is ok,my problem is solved.
            thanks

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              I suspected you were doing something similar. This is exactly the wrong way of doing it.
              See page 369 of http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • stackprogramerS Offline
                stackprogramerS Offline
                stackprogramer
                wrote on last edited by stackprogramer
                #7

                @VRonin
                i before write my code to this method 369 book:

                void WeatherBalloon::sendDatagram()
                {
                    QByteArray datagram;
                    QDataStream out(&datagram, QIODevice::WriteOnly);
                    out.setVersion(QDataStream::Qt_4_3);
                    out << QDateTime::currentDateTime() << temperature() << humidity()
                        << altitude();
                    udpSocket.writeDatagram(datagram, QHostAddress::LocalHost, 5824);
                }
                

                But we want to connect UDP processing data to GUI app, it can not read QDataStream Method and it Crashs. The app is written in C#,So we should try a method that work with VisualStudio.
                Agian Than kyou for attention
                best regards stackprogramer

                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