我遇到了一个我不太懂的警告.通过比较我认为是无符号的无符号和另一个无符号来生成警告.
这是来源:
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
int main()
{
uint8_t *arr = malloc(8);
assert(arr);
/* fill arr[] with stuff */
if (arr[3] != (uint8_t)(~arr[2])) { // Warning is here
/* stuff */
}
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
我使用以下过程构建的:
user@linux:~ $ gcc -o test -Wall -Wextra test.c
test.c: In function ‘main’:
test.c:13:16: warning: comparison of promoted ~unsigned with unsigned [-Wsign-compare]
Run Code Online (Sandbox Code Playgroud)
我正在使用gcc版本4.7.2 20121109(Red Hat 4.7.2-8)
我该如何解决上述比较?
小智 6
我遇到了同样的问题,我使用一个中间变量解决了这个问题:
uint8_t check = ~arr[2];
if (arr[3] != check)
Run Code Online (Sandbox Code Playgroud)