'ERROR : ISO C++ forbids conversion from string constant to char* [duplicate]

So, I have a program that uses the graphics mode [graphics.h] library... and I want to initialize the graph, So I would do this so naturally do this :

initgraph(graphics_driver,graphics_mode,"") ;

When I compile the above, it gives the error "ISO C++ forbids converting a string constant to char*"

I know one workaround for this :

char c_array[] = "" ; 
initgraph(graphics_driver,graphics_mode,c_array) ;

The above compiles just fine... This is ok with functions like initgraph().. because I will only be calling it once. But, I would like to use the outtextxy() function like this (Because I call it multiple times in my program) :

outtextxy(0,0,"Test") ;

Because declaring an array for all the different outtextxy() functions will just be a waste of space.

So, is there any way to use the above without arrays or any extra variables?

P.S: I am using codeblocks after installing the graphics.h library and configuring all the linker options. etc...



Solution 1:[1]

If you are absolutely sure that outtextxy() will not modify the string passed to it you could write you own wrapper function like:

void my_outtextxy(int x, int y, const char* text) {
  outtextxy(x, y, const_cast<char*>(text));
}

Solution 2:[2]

The file graphics.h to which you refer is positively ancient.

It's so old that it predates const.

String literals have been, for two decades, const char[N]. It was deprecated since then to pretend that they were char[N] instead. Since C++11 it is flat-out illegal. Thirteen years were given to migrate code from the old pre-const days, and there have been seven further years since.

You must either hack around this like you are now (copying the string literal to a would-be mutable buffer, even though it won't be mutated!), hack around it with a const_cast (be very sure that the argument won't be mutated, though!), or use a library from this millennium instead.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2