'Can't get pass C++ warning: (& crash after) deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

I tried everything, and I can't even start this simple graph code. I have installed the graphics.h, winbgim.h and libbgi.a in all the right folders. (include and lib). Linkers, too -lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32

I'm using CodeBlocks 16.01. I have also tried it in Dev-C++ with no luck.

First I got error:

C:\Program Files (x86)\CodeBlocks\MinGW\include\graphics.h|302|error: redefinition of 'int right'

But I solved that by changing the graphics.h file, int right needs to be replaced with int top.

Also there is 1 warning -

C:\Users\Davor\Desktop\New Project\drugi\main.cpp||In function 'int main()':| C:\Users\Davor\Desktop\New Project\drugi\main.cpp|15|warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]| ||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

After that, when I press 'Build and run' i get

" *.exe has stopped working. " In the background I first get -

Process returned 255 (0xFF) execution time : 91.666 s Press any key to continue.

But every other time I get -

Process returned -1073741819 (0xC0000005) execution time : 26.494 s Press any key to continue.

#include <iostream>
#include <cstdlib>
#include <graphics.h>


using namespace std;



int main() {

    int gdriver = 9;
    int gmode = 2;
    int x;
    initgraph(&gdriver, &gmode, "");
    setbkcolor (WHITE);
    setcolor (BLACK);
    cleardevice();
    for (x = 260; x < 364; x = x + 5)
    {
        circle(x,240,60);
    }
    getch();
    closegraph();

    return 0;
}


Solution 1:[1]

I have found the solution.

The problem was in the next library:

libbgi.a

I have tried sever different ones, but at the end I found the right one. As soon as I replaced it with the old one - the graphic window opened and worked just fine, without crashes.

The warning:

deprecated conversion from string constant to 'char*' [-Wwrite-strings]|

is still there, but the code works perfectly.

Thanks for help anyway.

Cheers.

Solution 2:[2]

Without looking inside <graphics.h>, It would appear that initgraph() is declared as taking a (non-const) char* as the third parameter - suggesting that it is an output parameter, pointing to a writeable char array that initgraph() will write to.

As well as producing the warning you've quoted, passing the string constant "" as the argument would result in undefined behaviour, of which crashing is a perfect example.

While my remarks above are valid, after looking at the documentation I would surmise that the parameter is not an output parameter, and that the lack of const is simply poor API implementation - as is the apparent absence of any means to determine whether initialisation actually succeeded or not...

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 szentmihaly
Solution 2