【问题标题】:Compiler claims pointer is const when it is not used编译器声称指针在不使用时是 const
【发布时间】:2014-02-08 07:01:38
【问题描述】:

我的编译器给出了这个警告:

inlinedata.h:9:6:注意:预期为“char *”,但参数类型为“const char *”

int  inline_data_receive(char *data,int length);

我不明白为什么它声称“数据”是一个 const 指针,而不是写成 const char*。

【问题讨论】:

  • 你怎么称呼它?

标签: c gcc


【解决方案1】:

意思是 参数(您传入的数据)是const。例如,它可能是字符串文字。所以不要这样做:

ret = inline_data_receive("hello", len);

这样做

char str[] = "hello";
ret = inline_data_receive(str, len);

您需要这样做,因为该函数不保证它不会修改输入字符串。

【讨论】:

    【解决方案2】:

    编译器抱怨您将const char* 值传递给标记为char* 的值。基本上如下

    const char* c = ...;
    inline_data_receive(c, strlen(c));
    

    编译器抱怨c 是const char* 但需要char* 才能与参数data 对齐

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 2019-03-18
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多