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

时间:2016-02-03 20:25:24

标签: c++ arduino

我是编程新手,正在为一个班级的项目工作。我正在尝试使用键盘输入密码。我无法弄清楚"从字符串常量转换为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);

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

1 个答案:

答案 0 :(得分:1)

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

实际输出看起来像这样

C:\Users\mwhit_000\Desktop\Capstone\Security_System\Security_System2\Security_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. 

来自“sketch”on不是错误消息,它的构建状态。

让我们打破它。

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:\ Users \ mwhit_000 \ Desktop \ Capstone \ Security_System \ Security_System2 \ Security_Sy stem2.ino

行号:27

专栏:38

错误类型:警告

描述:不推荐使用从字符串常量转换为'char *'[-Wwrite-strings]

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

所以这就是字面上告诉你,你正在将一个常量字符串转换成一个非常数字符串,直到这里的怪物:Password password = Password( "1234" );

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

相关问题