int SumBetween(int low, int high)
{
int i;
int end;
int Ray[high - low];
int sum;
end = (high - low);
for (i = 1; i = end; i++) {
Ray[i] = low + i;
}
sum = 0;
for (i = 1; i = end; i++) {
sum = sum + Ray[i];
}
return sum;
}
The function above keep coming with this error:
main.c: In function 'SumBetween':
main.c:12:2: error: suggest parentheses around assignment used as truth value [-Werror=parentheses]
for (i = 1; i = end; i++) {
^
main.c:17:2: error: suggest parentheses around assignment used as truth value [-Werror=parentheses]
for (i = 1; i = end; i++) {
^
cc1: all warnings being treated as errors
what am I doing wrong?
Your problem is not technically error, but a warning. In your case, all warnings are represented as errors, because compiler detected possible problem in your code.
As seen, you have a problem in your for loop:
i = 0
as arrays starts with 0
in C According to your code, you should rewrite your for loops to
//Start with i = 0 and go till end variable
// 1 2 3
for (i = 0; i < end; i++) {
Ray[i] = low + i;
}
Technically you're for() statement is a endless loop, if end! = 0
end! = 0
. Assuming that high
is greater than low
(or values is equal), you can avoid the second loop by doing as follows:
int SumBetween(int low, int high)
{
int i, end, sum = 0;
end = (high - low);
for (i = 1; i != end; i++) {
sum += low + i;
}
return sum;
}
Btw, my proposal relies on the assumptions that you are need to reach end
value from i = 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.