警告:初始化从指针目标类型中丢弃 'const' 限定符
warning: initialization discards 'const' qualifier from pointer target type
我们正在升级遗留代码以使用更新版本的 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};
how to resolve this warning
将 menuScreen
指针转换为指向 const 指针的指针,这是最佳选择。 _menuMain_Strings_0
是用 __ATTR_PROGMEM__
声明的,这很可能意味着它存储在某些 "program memory" 中,而不是在 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 };
我们正在升级遗留代码以使用更新版本的 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};
how to resolve this warning
将 menuScreen
指针转换为指向 const 指针的指针,这是最佳选择。 _menuMain_Strings_0
是用 __ATTR_PROGMEM__
声明的,这很可能意味着它存储在某些 "program memory" 中,而不是在 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 };