I got warning:
Pe186 \"Pointless comparison of unsigned int with zero\"
when I tried to compile the following code:
The warning complains about your for
loop break condition clLoop >= 0
. The loop will end if clLoop
gets negative, but that will never happen for an unsigned int.
You should remove =
in
clLoop >= 0
Let's say your cpLoopStart is 5
.
Then, clLoop value in the further iteratiosn would be -
clLoop = 4;
clLoop = 3;
clLoop = 2;
clLoop = 1;
clLoop = 0;
clLoop = 0;
clLoop = 0;
clLoop = 0;
|
|
|
Infinite times.
do{} while() can help you to use unsigned types of variable for loop without integer overflow:
// main.c
#include <stdio.h>
int main(void)
{
int array[] = {1,2,3,4,5,6,7,8,9,10};
unsigned int size = sizeof(array)/sizeof(int); // size == 10;
unsigned int i = size;
do
{
i--;
printf("Index: %u, content: %d\n",i,array[i]);
} while(i > 0);
return 0;
}
And compile it with:
gcc -std=c11 -Wall -Wextra -Wpedantic main.c
Output:
Index: 9, content: 10
Index: 8, content: 9
Index: 7, content: 8
Index: 6, content: 7
Index: 5, content: 6
Index: 4, content: 5
Index: 3, content: 4
Index: 2, content: 3
Index: 1, content: 2
Index: 0, content: 1
I think you meant to say
for(clLoop = cpLoopStart; clLoop; clLoop--)
{ //Do something
}