Search code examples
cgccbgi

Loading graphic files


#include <graphics.h> //importing graphics
#include <conio.h>

int main()
{
    int gd=0,gm;
    initgraph(&gd,&gm," ");
    circle(100,80,20);
    getch();
    closegraph();
}

I typed the above code in Code::Blocks but it does not execute and rather it says

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

I have been trying for three days. Someone please help me out. I have installed winbgim.h and other necessary files for graphics but it's not helping. I have searched all possible websites...

The red color is indicating the error line


Solution

  • Preamble: I deleted and then undeleted this answer several times. There are other questions that address this subject, but most of them seem to be focused on C++, which has some differences from C in this area, or to have been closed as a dupe of such a question. Some have answers I don't care for, or that are incomplete. In a possiuble act of hubris, therefore, I am providing this answer, and I choose to provide it here.


    Since the only string constant I see in the code is the " " argument to initgraph(), the warning must arise from there. In that case, the third parameter to that function must be declared as type char *.

    In that case, there is nothing inherently wrong with the code you presented. According to the standard, string constants correspond to arrays of char, and such an array is automatically converted to a pointer to its first element in most contexts where it is evaluated, including when it appears as a function argument. That lines up perfectly with the function.

    The problem is that the standard also says that if a program attempts to modify the contents of a string literal then it produces undefined behavior. It is not uncommon for the actual manifestation of that to be a program crash. Therefore, although it is technically legal, you take a significant risk when you convert a string constant to a char *, because whoever handles the pointer may not know that it is unsafe to attempt to use it to modify what it points to. This problem does not arise if you convert to const char * instead.

    GCC wants to help you detect such problems. Therefore its -Wwrite-strings] option, when enabled, causes string constants to be treated as arrays of const char instead of arrays of char, and therefore to trigger warnings such as you encountered.

    You have at least three options:

    1. You could disable the -Wwrite-strings compilation option. This is probably the best approach for legacy code with well-tested behavior, but I do not recommend it otherwise.

    2. Provided that the function in fact does not attempt to modify the array that its parameter points to, directly or indirectly, you can change the function parameter to type const char *. This may require a cascade of other, similar changes to avoid additional, relevant warnings.

    3. Avoid passing a string literal to the function. Instead create and pass a mutable array of char. For example:

    int gd = 0, gm;
    char s[] = " ";
    
    initgraph(&gd, &gm, s);
    // ...
    

    Note in particular that there, although there is a string literal, it is used only to initialize the distinct, non-const array s.