-
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 -
@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, evenmalloc
).
On top of that, if you declare itconst
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 areCD
,TC
,SZE
,R_Code
,Star
andSFI
? -
thanks for reply CD and ... has 4byte length are int type and other int8_t type.
-
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 -
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 -
@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