更多 LValue 错误

发布于 2024-10-26 02:58:59 字数 1176 浏览 7 评论 0原文

下面是一个三重嵌套索引方案。我指向指针数组的指针在注释行上被取消引用......理论上这应该给我一个指针。我从中减去一,然后引用它并将其重新分配给指向指针数组的指针。但该行给出了操作数“&”的左值错误。

让我说得非常清楚。我想知道为什么这里发生错误,并获取一种方法,该方法可以将指针数组中前一个元素的地址分配给我的帧双指针。

我只会接受满足这两个标准的完整解决方案......

#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;

}

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿嘿嘿 2024-11-02 02:58:59
Frames[2].TestArrayPointer=
  &(*(Frames[2].TestArrayPointer)-1);

这里取消引用然后收回地址都是不必要的。你可以直接这样做 -

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

但这有一个潜在的问题。

  • 如果索引为0怎么办?运行时错误。
  • 如果索引是最后一个索引怎么办?内存泄漏。
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.
香草可樂 2024-11-02 02:58:59

您会收到错误,因为 -1 是在 * 之后应用的。您无法获取减法运算符结果的地址 - 例如右值,而不是左值(左值指定一个实际对象 - 右值只是短暂的价值)。

最初,Frames[2].TestArrayPointer 指向 TestPointerArray 指向的数组中的第三个 double *。据推测,您希望将其更改为指向该数组中的第二个 double *。如果是这样,您只需将该行更改为:

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

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;
夏末 2024-11-02 02:58:59

Frames[2].TestArrayPointer 的类型为 double**

*(Frames[2].TestArrayPointer) 的类型为 double*

所以 *(Frames[2].TestArrayPointer) - 1 是一个类型为 double* 的临时变量。临时变量是一个右值——你不能获取它的地址。

我认为你想要的是:

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

它也是一个 double*,但它是 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文