【问题标题】:Warning passing 'char []' to parameter of type 'unsigned char *'警告将“char []”传递给“unsigned char *”类型的参数
【发布时间】:2017-05-13 17:52:42
【问题描述】:

我正在为 ARM 控制器开发 Keil 编译器并用 C 编写代码。我被这个警告卡住了,我得到了这个:

warning : passing 'char [7]' to parameter of type 'unsigned char *'converts between pointers to integer types with different sign.

例行公事:

void WriteString(unsigned char *Msg_add)
    {
        for(Lcd_pointer=0; Lcd_pointer < 16; Lcd_pointer++)
        {
            Write_lcd_data(*Msg_add);
            Msg_add++;
            if(*Msg_add == '\0')
                break;
        }
    }

我正在使用随机字符串传递上述例程:

WriteString("Token:");

顺便说一句,这些功能有效,但我收到了这个警告。如何删除?

【问题讨论】:

  • WriteString((unsigned char*)"Token:");
  • 编译器可能有一个开关来允许指针到字符类型之间的隐式转换

标签: c arm keil


【解决方案1】:

这是预期的警告,因为 "Token:" 的类型为 const char *
void WriteString(unsigned char *Msg_add) 期待unsigned char *

您需要键入强制转换参数

WriteString((unsigned char*) your_data);

【讨论】:

  • 请注意,C 中的字符串文字不是 const 限定的,并且具有 char 数组 类型。在大多数情况下,自然会转换为 pointer to char
  • 我的意思不是该函数不应该采用 const(无符号)char 指针,而是说“as "Token:" is of type const char *" 具有误导性。请务必让函数采用 const 字符,如果他们不打算修改它们,这是合理的建议。你是正确的,你不能修改字符串文字,即使它们不是 const。
猜你喜欢
  • 2019-05-30
  • 2015-03-08
  • 1970-01-01
  • 2018-05-23
  • 2021-12-11
  • 2014-02-20
  • 2021-06-29
  • 2018-11-03
  • 2020-05-22
相关资源
最近更新 更多