从字符串到字符*的转换已弃用 [英] Conversion from String Literal to Char* is deprecated

查看:309
本文介绍了从字符串到字符*的转换已弃用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的代码中一直收到错误从字符串字面量转换为char *已弃用。代码的目的是使用指针指针为字符串1和字符串2分配一个字,然后将其打印出来。如何解决这个问题?



这是我的代码:

  #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<< 第一个字符串是:
<< teststrings
<< ,你的第二个字符串是:
<< * teststrings
<< endl;
}


解决方案

C ++字符串文字是



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



以下是您的代码编译时没有警告的版本:

  #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<< 第一个字符串是:
<< teststrings
<< ,你的第二个字符串是:
<< * teststrings
<< endl;
}

考虑如果语言 >施加此限制:

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

A(非 - const char * 允许您修改指针指向的数据。如果你可以给一个简单的 char * 赋值一个字符串文字(隐式转换为字符串的第一个字符的指针),你可以使用该指针修改编译器没有任何警告的字符串文字。上面的无效代码如果有效,将打印

  literal 

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


在C中,字符串字面量是一个 char 的字符串字面量的数组,不是 const char * 的数组,但试图修改它具有未定义的行为,这意味着在C中你可以合法地写 char * s =hello; s [0] ='H'; ,编译器不一定抱怨 - 但是当你运行程序时,这是为了保持与在引入 const 关键字之前编写的C代码的向后兼容性。C ++从 const 非常开始,所以这种特殊的妥协是没有必要的。)


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?

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;  
}

解决方案

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

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 << "\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

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).

(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.)

这篇关于从字符串到字符*的转换已弃用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆