C to C++, char * and QByteArray - General questions.
-
@artwaw said in C to C++, char * and QByteArray - General questions.:
strlcat
It's documented - so you have to look what your function you call needs.
"This means that for strlcpy() src must be NUL-terminated and for strlcat() both src and dst must be NUL-terminated."
"The strlcat() function appends the NUL-terminated string src to the end of dst. It will append at most size - strlen(dst) - 1 bytes, NUL-terminating the result."
-
@Christian-Ehrlicher Yes, I have no problems with strlcat - my question was rather as to how QByteArray does append(). Sorry if that was not clear.
-
It simply adds the character to the end - what should it do else? It's an array of bytes so appending a '\0' will append this too.
-
@artwaw said in C to C++, char * and QByteArray - General questions.:
I found docs inconclusive: if I am to append
char *
(null terminated) to already null terminated QByteArray that way - will the trailing\0
be removed before append or left and I'll end up with two\0
in the array?You won't end up with two
\0
in the array.Here is the example at https://doc.qt.io/qt-5/qbytearray.html#append extended:
QByteArray x1("free"); QByteArray y1("dom"); x1.append(y); QByteArray x2("free"); x2.append("dom"); // appending const char*
Both
x1
andx2
will contain "freedom" (or more precisely,"freedom\0"
)Conventionally in C,
\0
is a marker that means "end of string". So, if you have a char array that contains"free\0dom\0"
most C functions will see that as a 4-character string ("free"). -
@JKSH said in C to C++, char * and QByteArray - General questions.:
So, if you have a char array that contains "free\0dom\0" most C functions will see that as a 4-character string ("free").
Thank you for clarifying. I was aware of the quoted fact, that is mentioned in QByteArray doc. Just how those are concatenated was not clear to me.
-
@artwaw said in C to C++, char * and QByteArray - General questions.:
Just how those are concatenated was not clear to me.
The easiest way to find out is to write the code and try it :-)
-
@artwaw
Mostly, just forget thatQByteArray
appends an extra\0
at the end. It's mostly for your convenience so it can be read as a string. When appending, ignore it, letQByteArray
put an extra\0
at the end for you (and let it overwrite the\0
which was there before). -
QByteArray is a simple std::vector<char> with an explicit \0 at the end.