void foo(void **Pointer);
int main ()
{
int *IntPtr;
foo(&((void*)IntPtr));
}
Why do I get an error?
error: lvalue required as unary ‘&’ operand
Thanks
void foo(void **Pointer);
int main ()
{
int *IntPtr;
foo(&((void*)IntPtr));
}
Why do I get an error?
error: lvalue required as unary ‘&’ operand
Thanks
void foo(void **Pointer);
int main ()
{
int *IntPtr;
foo((void**)&IntPtr);
}
When you do
(void*)IntPtr
you create a temporary variable, which is only an rvalue, and so can't be dereferenced.
What you need to do is:
int main()
{
int* IntPtr;
void* VoidPtr = (void*)IntPtr;
foo(&VoidPtr);
}
or equivalent
(void*) is not an lvalue, it is kind of a casting operator, you need to have the ampersand to the immediate left of the variable (lvalue). This should be right:
foo(((void**)&IntPtr));
More C++ style:
foo( reinterpret_cast< void** >( IntPtr ) );
But remember, according to Standard such a cast is implementation specific. Standard gives no guarantees about behavior of such cast.
As others point out, you have the order of the cast and the &
wrong. But why do you use void**
at all? That means that you accept a pointer to a void pointer. But that's not at all what you want. Just make the parameter a void*
, and it will accept any pointer to some object:
void foo(void*);
int main () {
int *IntPtr;
foo(&IntPtr);
assert(IntPtr == NULL);
}
That's what void*
is for. Later, cast it back using static_cast
. That's a quite restrictive cast, that doesn't allow dangerous variants, unlike the C style cast (type):
void foo(void* p) {
int** pint = static_cast<int**>(p);
*pint = NULL;
}
If the function takes pointers to void*
, then that function can't accept pointers to int*
. But if the function accepts either or, then the function should accept void*
, and you should cast to the proper type inside the function. Maybe paste what you really want to do, we can help you better then. C++ has some good tools available, including templates and overloading, both of which sound helpful in this case.