c++ - 数组一直只返回最后一个元素。 [C/阿杜伊诺]

标签 c++ c arrays pointers arduino

我在 Arduino 上遇到一个数组(称为“GeneralInput”类型的“输入”)的问题,基本上,无论我尝试访问哪个元素,代码总是返回该数组的最后一个元素。 这是部分代码:

//...include statements
//other initializations
GeneralInput *Inputs[19];

void setup() 
{
    //...
    //...
    InitializeInputs();
}

void InitializeInputs()
{
    //type 0 = pedal switch;  1 = volume pedal
    //type 2 = potentiometer;   3= switch;
    //pedal switches
    Inputs[0] = &GeneralInput(0,0,true,false,NULL,10);
    Inputs[1] = &GeneralInput(1,0,true,false,NULL,9);
    Inputs[2] = &GeneralInput(2,0,true,false,NULL,6);
    Inputs[3] = &GeneralInput(3,0,true,false,NULL,5);
    //volume pedal
    Inputs[4] = &GeneralInput(4,1,false,false,NULL,A2);
    //potentiometer
    Inputs[5] = &GeneralInput(5,2,false,true,mux2,5);
    Inputs[6] = &GeneralInput(6,2,false,true,mux2,6);
    Inputs[7] = &GeneralInput(7,2,false,true,mux2,7);
    Inputs[8] = &GeneralInput(8,2,false,true,mux2,8);
    Inputs[9] = &GeneralInput(9,2,false,true,mux2,9);
    Inputs[10] = &GeneralInput(10,2,false,true,mux2,10);
    Inputs[11] = &GeneralInput(11,2,false,true,mux2,11);
    //switch
    Inputs[12] = &GeneralInput(12,3,true,true,mux2,15);
    Inputs[13] = &GeneralInput(13,3,true,true,mux2,14);
    Inputs[14] = &GeneralInput(14,3,true,true,mux2,13);
    Inputs[15] = &GeneralInput(15,3,true,true,mux2,12);
    //joystick   
    Inputs[16] = &GeneralInput(16,3,true,true,mux1,2);  //switch
    Inputs[17] = &GeneralInput(17,2,false,true,mux1,1); //x axis
    Inputs[18] = &GeneralInput(18,2,false,true,mux1,3); //y axis
}

void loop() 
{  
    int length=0;
    //cycle through different inputs
    int startIndex=0,endIndex=0;
    //temp arrays
    byte toSendTmp[30]; 
    for(int i=0;i<30;i++)
      toSendTmp[i]=0;
    //...
    //..
    int packetIndex=0;
    for(int i=startIndex;i<endIndex;i++)
    {
         //if the input is updated,fill the array with the new data
         /*
          * When i try to have access to the i-element i always get
          * the last one instead.
          */
         if(Inputs[i]->Update())
         {
            toSendTmp[(packetIndex*3)] = Inputs[i]->GetID(); 
            toSendTmp[(packetIndex*3)+1] = Inputs[i]->GetType(); 
            toSendTmp[(packetIndex*3)+2] = Inputs[i]->GetValue();
            packetIndex++;
         }         
    }
    //....
    //...
}

如果需要,这里是 GeneralInput.hGeneralInput.cp p代码。 注意:我不知道数组是否总是返回最后一项,或者数组的每个槽是否都填充了指向同一对象(最后创建的对象)的指针。

知道我做错了什么吗?

提前致谢。

最佳答案

你的 &GeneralInput 是不正确的,实际上你创建了临时对象并将它们的地址存储在一个数组中,但是一旦你的 GeneralInput 对象被销毁(与创建相同的行), 一个新的对象发生在相同的地址:

// Create GeneralInput at address @
Inputs[0] = &GeneralInput(0,0,true,false,NULL,10);
// End of your temporary object, the `GeneralInput` object is destroyed but you still
// points to its address...
/* etc. */

您得到的是最后一个值,因为编译器总是在同一地址创建 GeneralInput,因此所有 Inputs[] 都指向同一地址。

您需要动态创建您的GeneralInput:

Inputs[0] = new GeneralInput(0,0,true,false,NULL,10);

关于c++ - 数组一直只返回最后一个元素。 [C/阿杜伊诺],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24654293/

相关文章:

c - 不同的数组大小

java - 考虑内存的文件比较

arrays - 将 NULL 数组填充到自定义聚合函数的最大长度 - 无 array_fill 函数

c++ - 使用模板标签的决定

c++ - QT Creator 中的部署和静态打包在单个可执行文件中链接 QT 和 OpenCV

c++ - 仅检索对象的类型

c++ - 是否允许以不同于调用 allocator::allocate() 的顺序调用 allocator::deallocate()?

c - 开发分布式系统时,GCC 工具链的哪些元素应该相同?

c++ - 在两个不同的无符号整数值之间左移

python - 创建一个 numpy.array,其元素由子类 numpy.ndarrays 组成