编译C文件时出现以下错误:
t_memmove.c: In function ‘ft_memmove’:
ft_memmove.c:19: warning: comparison of unsigned expression >= 0 is always true
Run Code Online (Sandbox Code Playgroud)
这是完整的代码,通过cat ft_memmove.c:
#include "libft.h"
#include <string.h>
void *ft_memmove(void *s1, const void *s2, size_t n)
{
char *s1c;
char *s2c;
size_t i;
if (!s1 || !s2 || !n)
{
return s1;
}
i = 0;
s1c = (char *) s1;
s2c = (char *) s2;
if (s1c > s2c)
{
while (n - i >= 0) // this triggers the error
{
s1c[n - i] = s2c[n - i];
++i;
}
}
else
{
while (i < n)
{
s1c[i] = s2c[i];
++i;
}
}
return s1;
}
Run Code Online (Sandbox Code Playgroud)
我确实理解size_t是无符号的,并且由于这个原因,两个整数都将> = 0.但是因为我从另一个中减去一个,所以我不明白.为什么会出现这个错误?
| 归档时间: |
|
| 查看次数: |
8182 次 |
| 最近记录: |