QByteArray::data() has an overload due to the constancy of the QByteArray object for which this function is called.
If the context of using the result of a call to data() implies the possibility of changing the data through the received pointer, then you need to use the non-constant QByteArray object and use the char* type for the variable. If this causes an error as in your question, then you are going to change the data of a constant object. This indicates an error in the design of the enclosing class (assuming that QByteArray in your case a member of the class).
If the result of data() not supposed to be changed, then you can ignore the constancy of the QByteArray object, but simply use const char* for the variable.
Attempting to remove constancy via const_cast can end badly (UB) if the object used is really constant.
An example relevant to any type of T :
T obj; const T& cref = T; T& ref = const_cast<T&>(cref); // OK, т.к. cref реально ссылается на неконстантный объект const T obj; const T& cref = T; T& ref = const_cast<T&>(cref); // Плохо, т.к. cref реально ссылается на константный объект
An attempt to modify obj through the ref reference in the second case leads to undefined behavior.
Another way to avoid an error in the TempPtrBuff initialization TempPtrBuff is to use auto :
auto TempPtrBuff = ARes.data();
But it still does not give the ability to modify a constant object.
const char * TempPtrBuff = ARes.data();orchar * TempPtrBuff = const_cast<char*>ARes.data();depending on what you want to get. - Embedder