从字符串常量转换为字符*
conversion from string constant to char*
我是编程新手,正在为一个班级做一个项目。我正在尝试使用键盘输入密码。我无法弄清楚"我不断得到的"从字符串常量到 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_System2.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"更改为其他内容,则结果未定义。程序可能会崩溃。它可以继续运行并在以后崩溃。它可以吃掉一只猫。
相关文章:
- 从矢量<无符号字符>转换为字符* 包括垃圾数据
- 尝试将字符串/字符转换为整数会产生意外结果
- 如何使用C++将字符串中的字符转换为整数变量
- 字符转换功能 std::isupper() & std::islower() C++17
- 将字符 * 转换为字符串 *
- 在编译时将常量字符* 转换为常量 char_type*
- 如何在 C/C++ 中将无符号字符*转换为无符号字符数组?
- 错误:请求从"常量字符 [5]"转换为非标量类型"字符串"
- 无法使用字符串流将字符转换为字符串C++
- 将无符号字符 C++ 转换为 C#
- C++:使用没有位移位的指针将无符号字符转换为无符号 int
- 为什么我可以隐式地将字符*转换为常量字符*,但不能将无符号字符*
- 无法<string>从"常量字符 []"转换为<类名>
- 不能将字符转换为整数吗?
- 编译时将字符*转换为字节
- CP1251:从字符* 转换为 wchar_t* 时的编码失真
- 将单个字符转换为 std::string 前缀 \x01
- PPM 将字符转换为 int 授予负数
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类
- 将常量字符* 转换为字符时出错