Below is a triple nested indexing scheme. My pointer to an array of pointers is dereferenced on the commented line... in theory this should give me a pointer. I subtract one from it and then reference it and reassign that to my pointer to the array of pointers. But that line gives an lvalue error for the operand "&".
Let me be perfectly clear. I want to both know why the error is occurring here AND get a method that works for assigning the address of the previous element in the array of pointers to my frame double pointer.
I will only accept full solutions that satisfy both criteria....
#include <iostream>
typedef struct frame_s {
double ** TestArrayPointer;
} frame_t;
main () {
double * TestArray;
double ** TestPointerArray;
TestArray = new double [100];
TestPointerArray = new double * [100];
for (unsigned int Counter = 0; Counter<100; Counter++)
{
TestArray[Counter]=Counter;
TestPointerArray[Counter]=&(TestArray[Counter]);
}
frame_t Frames[10];
for (unsigned int Counter = 0; Counter<10; Counter++)
Frames[Counter].TestArrayPointer = &(TestPointerArray[Counter*10]);
//Move pointer to point at array position one back.
Frames[2].TestArrayPointer=
&(*(Frames[2].TestArrayPointer)-1); //error! here <--
//OUTPUT Values...
for (unsigned int Counter = 0; Counter<100; Counter++)
std::cout << "P: " << TestPointerArray[Counter] << " V: "
<< *(TestPointerArray[Counter]) << std::endl;
}
Frames[2].TestArrayPointer
has typedouble**
*(Frames[2].TestArrayPointer)
has typedouble*
So
*(Frames[2].TestArrayPointer) - 1
is a temporary with typedouble*
. A temporary is an rvalue - you can't take the address of it.I think what you want is:
which is also a
double*
, but it's the one previous toFrames[2].TestArrayPointer
rather than a temporary.And if we get rid of 'canceling' operators and redundant parens:
Here both the dereferencing and then taking back the address is unnecessary. You can directly do -
But this has a potential problems.
You get the error because the
-1
is applied after the*
. You can't take the address of the result of the subtraction operator - such is an rvalue, rather than an lvalue (an lvalue designates an actual object - an rvalue is just an ephemeral value).Initially,
Frames[2].TestArrayPointer
points at the thirddouble *
in the array pointed to byTestPointerArray
. Presumably, you want to change it to point to the seconddouble *
in that array instead. If so, you can simply change that line to: