[英]What am i doing wrong in my c function
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: 如图所示,您在for循环中遇到问题:
i = 0
as arrays starts with 0
in C i = 0
因为数组在C中以0
开头 According to your code, you should rewrite your for loops to 根据您的代码,您应该将for循环重写为
//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
,则您的for()语句是一个无穷循环end! = 0
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: 假设
high
于low
(或值相等),则可以通过执行以下操作避免第二个循环:
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
顺便说一句,我的建议基于以下假设:您需要从
i = 1
达到end
值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.