从字符串常量转换为字符*

conversion from string constant to char*

本文关键字:字符 转换 字符串 常量      更新时间:2023-10-16

我是编程新手,正在为一个班级做一个项目。我正在尝试使用键盘输入密码。我无法弄清楚"我不断得到的"从字符串常量到 char* 的转换"错误。

这是我的代码:

Password password = Password( "1234" );
const byte ROWS = 4; //Four rows
const byte COLS = 4; //Four columns
char keys[ROWS][COLS] = {
{ '1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
byte rowPins[ROWS] = {
46, 47, 48, 49};  //connect to the row pinouts of the keypad
byte colPins[COLS] = {
50, 51, 52, 53};  //connect to the column pinouts of the keypad
//Create the Keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

任何帮助或想法将不胜感激

这不是一个答案,但到目前为止,评论无法解释OP发生了什么,所以这里有一个更长,更详细的镜头。

实际输出如下所示

C:Usersmwhit_000DesktopCapstoneSecurity_SystemSecurity_System2Security_Sy‌​stem2.ino:27:38: warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings] 
Password password = Password( "1234" ); 
                                      ^ 
Sketch uses 11,850 bytes (4%) of program storage space. Maximum is 253,952 bytes. Global variables use 1,022 bytes (12%) of dynamic memory, leaving 7,170 bytes for local variables. Maximum is 8,192 bytes. 

从"草图"开始不是错误消息,而是其构建状态。

让我们分解一下。

file name:line number:character on that line:type of error: error description 
problem line
^ marking where on the above line the compiler thinks the problem is

文件名: C:\用户\mwhit_000\桌面\顶点\Security_System\Security_System2\Security_Sy stem2.ino

行号:27

专栏: 38

错误类型:警告

说明:已弃用从字符串常量到"char*"的转换 [-Wwrite-strings]

错误行:Password password = Password( "1234" );

所以这从字面上看告诉你,你正在将一个常量字符串转换为一个非常量字符串,就像这里的怪胎一样:Password password = Password( "1234" );

为什么这很糟糕?"1234"是一个常量字符串。"1234"无法更改。密码采用非常量字符串,并且不承诺不会尝试更改字符串的值。如果 Password 尝试将"1234"更改为其他内容,则结果未定义。程序可能会崩溃。它可以继续运行并在以后崩溃。它可以吃掉一只猫。