更多 LValue 错误
下面是一个三重嵌套索引方案。我指向指针数组的指针在注释行上被取消引用......理论上这应该给我一个指针。我从中减去一,然后引用它并将其重新分配给指向指针数组的指针。但该行给出了操作数“&”的左值错误。
让我说得非常清楚。我想知道为什么这里发生错误,并获取一种方法,该方法可以将指针数组中前一个元素的地址分配给我的帧双指针。
我只会接受满足这两个标准的完整解决方案......
#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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里取消引用然后收回地址都是不必要的。你可以直接这样做 -
但这有一个潜在的问题。
Here both the dereferencing and then taking back the address is unnecessary. You can directly do -
But this has a potential problems.
您会收到错误,因为
-1
是在*
之后应用的。您无法获取减法运算符结果的地址 - 例如右值,而不是左值(左值指定一个实际对象 - 右值只是短暂的价值)。最初,
Frames[2].TestArrayPointer
指向TestPointerArray
指向的数组中的第三个double *
。据推测,您希望将其更改为指向该数组中的第二个double *
。如果是这样,您只需将该行更改为: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:Frames[2].TestArrayPointer
的类型为double**
*(Frames[2].TestArrayPointer)
的类型为double*
所以
*(Frames[2].TestArrayPointer) - 1
是一个类型为double*
的临时变量。临时变量是一个右值——你不能获取它的地址。我认为你想要的是:
它也是一个
double*
,但它是Frames[2].TestArrayPointer
之前的一个,而不是临时的。如果我们去掉“取消”运算符和冗余括号:
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: