我正在使用gnuplot在C 中绘制图形。该图是按预期的绘图,但在编译过程中有警告。警告是什么意思?
warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
这是我正在使用的功能:
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);
}
字符串文字是const char 的数组,我们可以从C 标准章节 2.14.5
string literals 中看到这一点(强调我的):
普通字符串文字和UTF-8字符串文字也称为狭窄的字符串文字。一个狭窄的字符串字面具有" n const char的数组" ,其中n是下面定义的字符串的大小,并且具有静态存储持续时间(3.7)。
因此,此更改将删除警告:
const char * commandsForGnuplot[] = {"set title "Probability Graph"", "plot 'data.temp' with lines"};
^^^^^
注意,允许一个*non-const char **指向 const 数据是一个坏主意,因为修改了 const 或a string literal 是不确定的行为。我们可以通过转到7.1.6.1
cv-Qualifiers 来看到这一点:
除了可以修改的任何班级成员都可以修改为突变(7.1.1), 任何尝试在其生命周期中修改const对象的尝试(3.8)结果 在未定义的行为中。
和 2.14.5
string Literals e节:
所有字符串文字是否不同(也就是存储在 非重叠对象)是定义的。的效果 试图修改字符串字面的文字不确定。