【问题标题】:warning: initialization discards 'const' qualifier from pointer target type警告:初始化从指针目标类型中丢弃“const”限定符
【发布时间】:2019-09-11 20:21:37
【问题描述】:

我们正在升级旧代码以使用新版本的 GCC。 (9.1) 我一直在寻找有关如何解决此警告的答案,但我非常无能并且一直在努力理解发生了什么。

我已经尝试删除一个常量以使其保持一致,但随后我收到一个错误,因为该结构需要声明为 ATTR_PROGMEM 的常量。我不得不在指针之后添加一个const 关键字也可以解决错误,但它变成了这个警告。


#include <avr/pgmspace.h>

typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned short UINT16;
typedef unsigned long DWORD;
typedef unsigned long UINT32;


typedef signed char CHAR;
typedef signed short INT;
typedef signed short INT16;
typedef signed long LONG;
typedef signed long INT32;


typedef float FLOAT;
typedef float SINGLE;

typedef char BOOLEAN;

typedef struct {

    BOOLEAN ReadOnly;
    // * Where to display field
    BYTE row;
    BYTE col;
    BYTE field_type;

    union {
        INT16 *i16_ptr;
        BOOLEAN *b_ptr;
        BYTE *str_index;
    } fields;

    struct {
        INT16 minrange;
        INT16 maxrange;
    } range;
    const char **textFields; // Table of text fields if field_type == FIELD_STRINGS
} MENU_FIELD;


typedef struct {
    /** the screen display */
    const char **menuScreen;  // Pointer to a list of string pointers
    const MENU_FIELD *menuFields; // A pointer to the first field definition
} MENU_DEFINITION;

static const char _menuMain_L1_0[] __ATTR_PROGMEM__ =   "    SetPoint  Actual";
/*01234567890      123456789 */
static const char _menuMain_L2_0[] __ATTR_PROGMEM__   = "Temp       \x01" "       \x01";

static const char _menuMain_L3_0[] __ATTR_PROGMEM__ =   "Rh         %" "       %";
static const char _menuMain_L4_0[] __ATTR_PROGMEM__ =   "DP      \x01" " ";

static const char * const _menuMain_Strings_0[] __ATTR_PROGMEM__ = { _menuMain_L1_0,
_menuMain_L2_0, _menuMain_L3_0, _menuMain_L4_0 };

// When you add this line, you get the const error
static const MENU_DEFINITION _menuDef_Main_0 __ATTR_PROGMEM__ = { _menuMain_Strings_0};


int main(void) {
    return 0;

}

这是警告输出:

../main.c:75:67: warning: initialization discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
   75 | static const MENU_DEFINITION _menuDef_Main_0 __ATTR_PROGMEM__ = { _menuMain_Strings_0};

【问题讨论】:

  • const char **menuScreen; 是一个指向 const 字符的指针。但是您传递了const char * const _menuMain_Strings_0[],它是一个指向 const 指向 const 字符的指针。您正在失去一个常量,因此是警告。请删除所有奇怪的typedefs 并使用标准的stdint.h 和stdbool.h。

标签: arrays gcc constants avr-gcc


【解决方案1】:

如何解决此警告

将menuScreen 指针转换为指向 const 指针的指针,这是最好的选择。 _menuMain_Strings_0 是用 __ATTR_PROGMEM__ 声明的,这很可能意味着它存储在某个“程序内存”中,而不是在 ram 中,这意味着它是不可变的,应该用 const 限定符声明。所以:

typedef struct {
    /** the screen display */
    const char * const *menuScreen;  // Pointer to a list of string pointers
    const MENU_FIELD *menuFields; // A pointer to the first field definition
} MENU_DEFINITION;

或更改_menuMain_Strings_0的类型:

static const char * const _menuMain_Strings_0[] __ATTR_PROGMEM__ = { _menuMain_L1_0,
_menuMain_L2_0, _menuMain_L3_0, _menuMain_L4_0 };

或者您可以将其转换为 void* 并使用意大利面条代码:

static const MENU_DEFINITION _menuDef_Main_0 __ATTR_PROGMEM__ = { (void*)_menuMain_Strings_0 };

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多