【问题标题】:Understanding the warning "comparison of promoted ~unsigned with unsigned"了解警告“provision ofprovided ~unsigned with unsigned”
【发布时间】:2013-08-26 15:02:36
【问题描述】:

我遇到了一个我不太明白的警告。警告是通过将我认为是一个未签名的内容与另一个未签名的内容进行比较来生成的。

这里是来源:

#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;
}

我使用以下过程构建:

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]

我正在使用 gcc 版本 4.7.2 20121109 (Red Hat 4.7.2-8)

如何解决上述比较问题?

【问题讨论】:

    标签: c gcc


    【解决方案1】:

    我遇到了同样的问题,我通过使用中间变量来解决它:

    uint8_t check = ~arr[2];
    if (arr[3] != check)
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多