C++ 不推荐从字符串文字转换为 char*
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13690306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Conversion from string literal to char* is deprecated
提问by Andrew T
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?
我在我的代码中不断收到错误“不推荐使用从字符串文字到 char* 的转换”。代码的目的是使用一个指向指针的指针给 string1 和 string2 分配一个单词,然后打印出来。我怎样才能解决这个问题?
Here is my code:
这是我的代码:
#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;
}
回答by Keith Thompson
C++ string literals are arrays of constchar
, which means you can't legally modify them.
C++ 字符串文字是const数组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*
.
如果要安全地将字符串文字分配给指针(这涉及隐式数组到指针的转换),则需要将目标指针声明为const char*
,而不仅仅是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'timpose this restriction:
考虑一下如果语言没有施加这个限制会发生什么:
#include <iostream>
int main() {
char *ptr = "some literal"; // This is invalid
*ptr = 'S';
std::cout << ptr << "\n";
}
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
A (non- const
)char*
允许您修改指针指向的数据。如果您可以将一个字符串文字(隐式转换为指向字符串第一个字符的指针)分配给一个 plain char*
,您将能够使用该指针修改字符串文字,而编译器不会发出警告。上面的无效代码,如果有效,将打印
Some literal
-- 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).
- 它实际上可能会在某些系统上这样做。但是,在我的系统上,它因分段错误而死亡,因为它尝试写入只读内存(不是物理 ROM,而是被操作系统标记为只读的内存)。
(An aside: C's rules for string literals are different from C++'s rules. In C, a string literal is an array of char
, notan 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.)
(旁白:C 的字符串文字规则与 C++ 的规则不同。在 C 中,字符串文字是 的数组char
,而不是的数组const char
-- 但尝试修改它具有未定义的行为。这意味着在 C 中您可以合法地编写char *s = "hello"; s[0] = 'H';
, 并且编译器不一定会抱怨——但是当你运行它时,程序很可能会因分段错误而死。这样做是为了保持与const
引入关键字之前编写的 C 代码的向后兼容性。C++const
从一开始就有,所以这种特殊的妥协是没有必要的。)