错误:表达式必须是可修改的l值

时间:2019-04-08 21:18:31

标签: c arrays pointers

我试图将数组的每个值乘以整数,但是我目前遇到以下错误:“表达式必须是可修改的左值”。这段代码的目的是,当按下按钮时,矩形将以2的标量因数放大。如何解决此错误?

void Draw_Rectangle(unsigned int *rectangle1)
{
    // Call draw rectangle function to draw rectangle 1
    GraphicsFunction_drawRectangle (*rectangle1, *(rectangle1 + 1));

}

int main (void)
{
    unsigned int rectangleOne[4] = {85, 5, 130, 20};
    // Call function to draw the rectangle in initial position
    Draw_Rectangle(&rectangleOne[0]);

   while(1) {

       if(*(SWITCH_ptr) == 512) {
        int i = 0;
        for(i = 0; i < 4; i++){
            &rectangleOne[i] = rectangleOne[i] * 1.5;
        }
        Draw_Rectangle(&rectangleOne[0]);
    }
   }
}

2 个答案:

答案 0 :(得分:2)

&rectangleOne[i] = rectangleOne[i] * 1.5; => rectangleOne[i] = rectangleOne[i] * 1.5;

it is enough also to Draw_Rectangle(rectangleOne);,因为数组是通过指针传递的

答案 1 :(得分:2)

更改:

&rectangleOne[i] = rectangleOne[i] * 1.5;

收件人:

rectangleOne[i] = rectangleOne[i] * 1.5;

或更简单地说,就是:

rectangleOne[i] *= 1.5;

原始版本的问题在于它试图分配给数组元素的地址,而不是数组元素本身。