更多LValue错误 [英] More LValue Errors

查看:145
本文介绍了更多LValue错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.


  • 如果索引为0,该怎么办?运行时错误。

  • 如果索引是最后一个索引,该怎么办?内存泄漏。

这篇关于更多LValue错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆