警告:初始化会从指针目标类型中丢弃“ const”限定符

时间:2019-09-11 20:21:37

标签: arrays gcc const avr-gcc

我们正在升级旧版代码,以与新版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};

1 个答案:

答案 0 :(得分:0)

  

如何解决此警告

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*并使用spagetti代码:

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