22

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?

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);
}
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Sagar
  • 273
  • 1
  • 3
  • 12
  • 7
    This is C, not C++ – Davidbrcz Feb 03 '14 at 13:54
  • It means that in a future version this will be a compile error. – PlasmaHH Feb 03 '14 at 13:55
  • 10
    @Davidbrcz First, it's still valid C++ code though it's C-style. Second, changing the tag to C is incorrect because this is not a problem that would happen to C. In C, string literals are not of `const` type. – Yu Hao Feb 03 '14 at 13:59
  • 2
    @Davidbrcz also please see [Retagging C++ questions as C without consulting asker](http://meta.stackexchange.com/questions/158450/retagging-c-questions-as-c-without-consulting-asker). – Shafik Yaghmour Feb 03 '14 at 14:01
  • @YuHao indeed, I also added the relevant meta thread on this topic. – Shafik Yaghmour Feb 03 '14 at 14:02
  • 2
    @Yu Hao: It is syntactically valid. However It is morally wrong to write that code and claim it is written in C++. If you had written this in my department, you would be immediately fired. My bad for retaging. But I still think it is wrong to let the C++ tag with it. This maintains the false idea you can do C++ like you do C. They are 2 separated languages and no questions should be tagged both because norms, idioms and tools are not the same. It is Either C either C++, not both. – Davidbrcz Feb 03 '14 at 14:09
  • 1
    It is not tagged both, it is tagged as C++. Also, C++ supports many different paradigms. IMO if a program is correct and tidy there is no basis for saying that the code is wrong because a different paradigm should have been used. OP's code is easy to read and understand. – M.M Apr 09 '14 at 01:47
  • I am just astonished by some of the comments I read on these sites. This guy asks a perfectly reasonable question, and someone just jumps down his throat. Why not make a polite, constructive comment and then answer it? In the UK, if you fired someone for that, the person would probably have an excellent case for taking you to an employment tribunal. – nmw01223 Aug 07 '16 at 05:02

1 Answers1

35

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

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

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 const data is a bad idea since modifying a const or a string literal is undefined behavior. We can see this by going to section 7.1.6.1 The cv-qualifiers which says:

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.

and section 2.14.5 String literals which says:

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.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740