开发者

More LValue Errors

开发者 https://www.devze.com 2023-02-19 03:22 出处:网络
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 i

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=
  &(*(Frames[2].TestArrayPointer)-1);

Here both the dereferencing and then taking back the address is unnecessary. You can directly do -

Frames[2].TestArrayPointer = (Frames[2].TestArrayPointer)-1) ;

But this has a potential problems.

  • What if the index is 0 ? Runtime error.
  • What if the index is the last index ? Memory leak.


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 third double * in the array pointed to by TestPointerArray. Presumably, you want to change it to point to the second double * in that array instead. If so, you can simply change that line to:

Frames[2].TestArrayPointer = Frames[2].TestArrayPointer - 1;


Frames[2].TestArrayPointer has type double**

*(Frames[2].TestArrayPointer) has type double*

So *(Frames[2].TestArrayPointer) - 1 is a temporary with type double*. A temporary is an rvalue - you can't take the address of it.

I think what you want is:

&(*((Frames[2].TestArrayPointer)-1))

which is also a double*, but it's the one previous to Frames[2].TestArrayPointer rather than a temporary.

And if we get rid of 'canceling' operators and redundant parens:

Frames[2].TestArrayPointer - 1
0

精彩评论

暂无评论...
验证码 换一张
取 消