理解警告“升级为未签名与未签名的比较". [英] Understanding the warning "comparison of promoted ~unsigned with unsigned"

查看:194
本文介绍了理解警告“升级为未签名与未签名的比较".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了我不太理解的警告.该警告是通过将我认为是一个未签名的对象与另一个未签名的对象进行比较而生成的.

I've encountered a warning that I don't really understand. The warning is generated by comparing what I consider to be one unsigned with another unsigned.

以下是来源:

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

我使用以下过程进行构建:

Which I build using the following procedure:

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)

I'm using gcc version 4.7.2 20121109 (Red Hat 4.7.2-8)

如何解决以上比较问题?

How can I fix the above comparison?

推荐答案

是的,我认为这是一个错误.阅读 [错误c/38341]升级后的〜unsigned与错误的警告比较未签名 ,以及为什么,警告:已签名和已签名之间的比较无符号整数表达式?.这可能会对您有所帮助.

Yes I think its a bug. Read [Bug c/38341] Wrong warning comparison of promoted ~unsigned with unsigned and also Why, warning: comparison between signed and unsigned integer expressions?. This might help you.

这篇关于理解警告“升级为未签名与未签名的比较".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆