0

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

genpfault
  • 51,148
  • 11
  • 85
  • 139
Samuel
  • 315
  • 3
  • 14

2 Answers2

6

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
2

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));
}
GSIO01
  • 495
  • 7
  • 13
  • @LightnessRacesinOrbit I also would prefere a modern api though. But sometimes that is impossible due to some reason so I thought I add this solution just in case. – GSIO01 Nov 05 '18 at 12:01
  • Worth mentioning for `const_cast(text)`... You can only cast away const if the original object *is* non-const. If you cast away const on an object that is const then undefined behavior. The rule protects things like strings in ROM where you truly cannot write to the memory. Also see [How do you cast away const'ness when the function takes a reference to the object](https://stackoverflow.com/q/9474741/608639) – jww Nov 05 '18 at 13:20
  • @jww: That is not true. You can `const_cast` to your heart's content; however, if you then perform a mutating operation on the expression, and it refers to something that is non-mutable, _that_ has undefined behaviour. For this reason `const_cast` is generally a bad and dangerous practice (because you took away from the type system the ability to actually tell you when you're going to do that UB thing); however, in cases like this it is justified and safe. The page you linked to does say this. – Lightness Races in Orbit Nov 05 '18 at 16:22