不推荐从字符串文字到 char* 的转换

编程入门 行业动态 更新时间:2024-10-23 08:27:01
本文介绍了不推荐从字符串文字到 char* 的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的代码中不断出现错误从字符串文字转换为 char* 已弃用".代码的目的是使用一个指向指针的指针来为 string1 和 string2 分配一个单词,然后将其打印出来.我怎样才能解决这个问题?

I keep getting the error "Conversion from string literal to char* is deprecated" in my code. The purpose of the code is to use a pointer-to-pointer to assign string1 and string2 a word, then print it out. How can I fix this?

这是我的代码:

#include <iostream> using namespace std; struct WORDBLOCK { char* string1; char* string2; }; void f3() { WORDBLOCK word; word.string1 = "Test1"; word.string2 = "Test2"; char *test1 = word.string1; char *test2 = word.string2; char** teststrings; teststrings = &test1; *teststrings = test2; cout << "The first string is: " << teststrings << " and your second string is: " << *teststrings << endl; }

推荐答案

C++ 字符串文字是 const char 的数组,这意味着你不能合法地修改它们.

C++ string literals are arrays of const char, which means you can't legally modify them.

如果您想安全地将字符串文字分配给指针(这涉及到隐式数组到指针的转换),则需要将目标指针声明为 const char*,而不仅仅是char*.

If you want to safely assign a string literal to a pointer (which involves an implicit array-to-pointer conversion), you need to declare the target pointer as const char*, not just as char*.

这是您的代码的一个版本,可以在没有警告的情况下编译:

Here's a version of your code that compiles without warnings:

#include <iostream> using namespace std; struct WORDBLOCK { const char* string1; const char* string2; }; void f3() { WORDBLOCK word; word.string1 = "Test1"; word.string2 = "Test2"; const char *test1 = word.string1; const char *test2 = word.string2; const char** teststrings; teststrings = &test1; *teststrings = test2; cout << "The first string is: " << teststrings << " and your second string is: " << *teststrings << endl; }

考虑一下如果语言没有施加这个限制会发生什么:

Consider what could happen if the language didn't impose this restriction:

#include <iostream> int main() { char *ptr = "some literal"; // This is invalid *ptr = 'S'; std::cout << ptr << " "; }

A (non-const) char* 允许您修改指针指向的数据.如果您可以将字符串文字(隐式转换为指向字符串第一个字符的指针)分配给纯 char*,则可以使用该指针来修改字符串文字,而无需来自编译器的警告.上面的无效代码,如果有效,将打印

A (non-const) char* lets you modify the data that the pointer points to. If you could assign a string literal (implicitly converted to a pointer to the first character of the string) to a plain char*, you'd be able to use that pointer to modify the string literal with no warnings from the compiler. The invalid code above, if it worked, would print

Some literal

-- 它实际上可能在某些系统上这样做.但是,在我的系统上,它会因分段错误而死,因为它尝试写入只读内存(不是物理 ROM,而是被操作系统标记为只读的内存).

-- and it might actually do so on some systems. On my system, though, it dies with a segmentation fault because it attempts to write to read-only memory (not physical ROM, but memory that's been marked as read-only by the operating system).

(顺便说一句:C 对字符串字面量的规则与 C++ 的规则不同.在 C 中,字符串字面量是 char 的数组,不是 const char -- 但试图修改它有未定义的行为.这意味着在 C 中你可以合法地编写 char *s = "hello"; s[0] = 'H';,编译器不一定会抱怨——但是当你运行它时,程序很可能会因为分段错误而死掉.这样做是为了保持与 const 关键字之前编写的 C 代码的向后兼容性被引入.C++ 从一开始就有 const,所以这种特殊的妥协是没有必要的.)

(An aside: C's rules for string literals are different from C++'s rules. In C, a string literal is an array of char, not an array of const char -- but attempting to modify it has undefined behavior. This means that in C you can legally write char *s = "hello"; s[0] = 'H';, and the compiler won't necessarily complain -- but the program is likely to die with a segmentation fault when you run it. This was done to maintain backward compatibility with C code written before the const keyword was introduced. C++ had const from the very beginning, so this particular compromise wasn't necessary.)

更多推荐

不推荐从字符串文字到 char* 的转换

本文发布于:2023-07-28 14:11:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1231652.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   文字   char

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!