30

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

这是我的代码:

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

1 回答 1

50

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

如果您想安全地将字符串文字分配给指针(这涉及到隐式数组到指针的转换),您需要将目标指针声明为const char*,而不仅仅是声明为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 << "The first string is: "
         << teststrings
         << " and your second string is: "
         << *teststrings
         << endl;
}

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

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

A(非constchar*允许您修改指针指向的数据。如果您可以将字符串文字(隐式转换为指向字符串第一个字符的指针)分配给 plain char*,您将能够使用该指针来修改字符串文字,而不会来自编译器的警告。上面的无效代码,如果有效,将打印

Some literal

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

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

于 2012-12-03T20:02:51.797 回答