[英]lvalue required as increment operand (2)
它給我這個錯誤*(repetitions + x)++;
在此for循環中。 有什么線索嗎?
for (int y = 0; y<hours; y++)
{
if (*(array + x) == *(array + y))
{
*(repetitions + x)++;
}
}
您不能增加右值repetitions + x
。 這與編寫相同的錯誤:
int a = 3;
int b = 2;
(a+b)++; // ????
++
運算符需要一個左值,即變量的名稱。 a+b
是一個臨時結果,沒有內存地址,無法遞增。
您可能打算編寫(*(repetitions + x))++;
,可以更清楚地表示為repetitions[x]++;
它解析為*((repetitions + x)++)
-也就是說,它嘗試修改一個常量地址,然后對其取消引用。 大概您打算增加地址所指向的內容。
您可以通過幾種不同的方法來做到這一點。 一種是使用括號。 另一個使用預增量的方法:
++*(repetitions + x);
要么:
(*(repetitions + x))++;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.