More LValue Errors

那年仲夏 提交于 2019-12-25 03:08:45

问题


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;

}

回答1:


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.



回答2:


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;



回答3:


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


来源:https://stackoverflow.com/questions/5412601/more-lvalue-errors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!