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

  • 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];

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

        std::cout << data;

    it reterns nothing,
    when i used this method:


    it crashed with this message:

    Press <RETURN> to close this window...

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

  • @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?

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

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

    This has nothing to do with Qt

  • 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:


    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.

  • I suspected you were doing something similar. This is exactly the wrong way of doing it.
    See page 369 of

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

    void WeatherBalloon::sendDatagram()
        QByteArray datagram;
        QDataStream out(&datagram, QIODevice::WriteOnly);
        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