Question

I have two programs that communicate over the D-Bus. In one of the there is a function that takes two parameters namely const char* data and int size and in another one I have a function that returns a value of the type unsigned char. I place a function call in the first program like this function((char*)program2->function().value(), 1) but i get the warning cast to pointer from integer of different size [-Wint-to-pointer-cast]. how should i resolve this warning? also I'm using Qt libraries.

EDIT:

actually in the function that takes the const char* and int I append the data to a QByteArray which accepts const char* and a size of type int in it's constructor as provided by Qt and the other program is supposed to return a number in the range 0-255 hence the unsinged char. If what I'm doing is wrong what's the best way to obtain the desired result?

Also call to program2->function invokes the following:

inline QDBusPendingReply<uchar> type()
    {
        QList<QVariant> argumentList;
        return asyncCallWithArgumentList(QLatin1String("type"), argumentList);
    }

this class has a function called value() which invokes:

inline typename Select<0>::Type value() const
    {
        return argumentAt<0>();
    }

NOTE: my main purpose of this question was to find out a way to create a reference to the returned result of calling a function defined within the proxy class not how to convert it to something acceptable for creation of a QByteArray so please stop adding unrelated tags like QByteArray or QtCore etc.

Was it helpful?

Solution

What you need is the address of the data... which would look like

function2(& program2->function().value(), 1);

and would be perfectly safe, since the lifetime of the temporary variable is until the end of the complete expression, which is long enough1 for function2 to use it.

Unfortunately, you can't use the & address-of operator on an rvalue. But there is a workaround, since you can bind a const lvalue reference to an rvalue, and you want a pointer to const data anyway:

template<T>
const T* get_addr(const T& v) { return &v; }

function2( get_addr(program2->function().value()), 1 );

If you get a signed-vs-unsigned, mismatch, try

function2( get_addr<char>(program2->function().value()), 1 );

Of course this is a lot of work just to avoid giving the variable a name, as Captain Obvlious has done.


1 Unless function2 saves that pointer for later, but if it does then you shouldn't be using a local variable to hold the value either, but pay very close attention to object lifetime.

OTHER TIPS

I do not think what you are doing is safe, hence your code might not work at all. If your function() is returning an "unsigned char", its going to make a copy of it and return it. At the same time your other function() is taking a "const char*". I suggest to modify your program2->function() to return a "char *".

Converting an integer to a pointer is not a really good idea especially if you don't know why the conversion is necessary and what exactly it does. It looks like the conversion isn't even necessary and you really need a solution to properly call the function instead of fixing the cast warning. From the code and description you provided it appears you want to send a single byte returned by the call to program2->function(). Since the value returned by this function is an rvalue you can't directly take it's address. Instead you can to store it in a variable and then pass a pointer to that instead. Something like the code below should let you do what you want.

char value = program2->function();
function(&value, 1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top