C++ 警告:不推荐将字符串常量转换为 'char*' [-Wwrite-strings]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21529194/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:41:29  来源:igfitidea点击:

C++ warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

c++string-literals

提问by Sagar

I am using gnuplot to draw a graph in C++. The graph is being plot as expected but there is a warning during compilation. What does the warning mean?

我正在使用 gnuplot 在 C++ 中绘制图形。该图正在按预期绘制,但在编译期间出现警告。警告是什么意思?

warning: deprecated conversion from string constant to ‘char*' [-Wwrite-strings]

This is the function I am using:

这是我正在使用的功能:

void plotgraph(double xvals[],double yvals[], int NUM_POINTS)
{
    char * commandsForGnuplot[] = {"set title \"Probability Graph\"", 
        "plot     'data.temp' with lines"};
    FILE * temp = fopen("data.temp", "w");
    FILE * gnuplotPipe = popen ("gnuplot -persistent ", "w");
    int i;
    for (i=0; i < NUM_POINTS; i++)
    {
        fprintf(temp, "%lf %lf \n", xvals[i], yvals[i]); 
        //Write the data to a te  mporary file
    }
    for (i=0; i < NUM_COMMANDS; i++)
    {
        fprintf(gnuplotPipe, "%s \n", commandsForGnuplot[i]); 
        //Send commands to gn  uplot one by one.
    }
    fflush(gnuplotPipe);
}

回答by Shafik Yaghmour

String literals are an array of const char, we can see this from the draft C++ standard section 2.14.5String literalswhich says (emphasis mine):

字符串文字是一个const char 数组,我们可以从 C++ 标准草案部分2.14.5字符串文字中看到这一点,它说(强调我的):

Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7).

普通字符串文字和 UTF-8 字符串文字也称为窄字符串文字。窄字符串字面量的类型为“array of n const char”,其中 n 是如下定义的字符串大小,并且具有静态存储持续时间 (3.7)。

so this change will remove the warning:

因此此更改将删除警告:

const char * commandsForGnuplot[] = {"set title \"Probability Graph\"", "plot     'data.temp' with lines"};
^^^^^

Note, allowing a *non-const char** to point to constdata is a bad idea since modifying a constor a string literalis undefined behavior. We can see this by going to section 7.1.6.1The cv-qualifierswhich says:

请注意,允许 *non-const char** 指向const数据是一个坏主意,因为修改const字符串文字未定义的行为。我们可以通过转到7.1.6.1cv-qualifiers部分来看到这一点,其中说:

Except that any class member declared mutable (7.1.1) can be modified, any attempt to modify a const object during its lifetime (3.8) results in undefined behavior.

除了任何声明为 mutable (7.1.1) 的类成员都可以修改之外,任何在其生命周期 (3.8) 期间修改 const 对象的尝试都会导致未定义的行为。

and section 2.14.5String literalswhich says:

和部分2.14.5字符串文字它说:

Whether all string literals are distinct (that is, are stored in nonoverlapping objects) is implementation defined. The effect of attempting to modify a string literal is undefined.

是否所有字符串文字都是不同的(即存储在非重叠对象中)是实现定义的。尝试修改字符串文字的效果是未定义的。