-4

What's wrong with this code?

#include <stdio.h>
#include <dos.h>
#include <graphics.h>

int main()
    {
        int gd = DETECT, gm;
        initgraph(&gd, &gm, ""); /* this is where the error it says */ 
    }

and it says this error:

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

I'm using codeblocks, and just trying to run the code that I have from my friend for my lecture.

genpfault
  • 51,148
  • 11
  • 85
  • 139
ndrhmt
  • 1
  • 1
  • 2
  • 4
    Tagged C and C++, but looks like straight C (did you know that C != C++?) – crashmstr Jan 19 '15 at 14:52
  • 1
    in your code there i don't see a single declaration of `char *` type. – Iharob Al Asimi Jan 19 '15 at 14:54
  • @crashmstr Seems C and but is also C++ code? Explain. – nbro Jan 19 '15 at 14:55
  • 2
    You don't specify the line of code in which the warning occurs and the related code. – Neil Kirk Jan 19 '15 at 14:56
  • @Rinzler The warning would be treated different by a c and a c++ compiler. – Iharob Al Asimi Jan 19 '15 at 14:56
  • 2
    @Rinzler I'm saying that it *does not* look like C++, but the OP has tagged it as such along with the C tag. Unless one is asking about a comparison or something that applies to both. In this case, compiling in C and compiling in C++ might be different as the languages are not the same. – crashmstr Jan 19 '15 at 14:58
  • the error is on line 11, if i'm running it on C it has a problem with the graphics.h. then i'm running it on c++ it has this error. – ndrhmt Jan 19 '15 at 15:04
  • @iharob: That was me. As far as I can see, it's an MCVE now and I didn't invalidate the answer. That this is C++, not C, I assumed because the header apparently doesn't work with a C compiler (see Ndrhmt's last comment). – mafso Jan 19 '15 at 15:14
  • @mafso sorry, I didn't see that the edit was from you. – Iharob Al Asimi Jan 19 '15 at 15:19
  • No problem, @iharob, but do you disagree with the edit (also [at] ndrhmt)? It shouldn't matter who did it, after all. I didn't intend to deface anything, just wanted to remove the unnecessary code. Some of your comments no longer apply to the question as-is, but they aren't answers and still may help OP (who knows the question as it was before), I don't consider this a problem. – mafso Jan 19 '15 at 15:40
  • 1
    @mafso No I don't disagree. Yes and it's true I wish the OP himself posted this very simple example exhibiting the problematic behavior. – Iharob Al Asimi Jan 19 '15 at 15:41

2 Answers2

3

From C++98, conversion of string literals to char* has been deprecated and has been completely removed in C++11.

This is because string literals are const char[] in C++98 and higher. While your code does not show all that is required, that is the reason for the warning.

If you are using C, this is perfectly fine since string literals in C are simply char[].

Pradhan
  • 16,391
  • 3
  • 44
  • 59
  • since string literals in c are simply `char[]`? can you please explain. – Iharob Al Asimi Jan 19 '15 at 14:57
  • @iharob The type of string literal in `C` is `char[]`. However, assigning a string literal to a `char*` and trying to modify it is UB. C++11 simply makes it illegal. – Pradhan Jan 19 '15 at 15:02
  • 2
    This OP didn't deserve the answer, because the posted code, doesn't even contain the error, but you gave a good answer anyway. – Iharob Al Asimi Jan 19 '15 at 15:03
1

This is caused by the line

initgraph(&gd, &gm, "");

as the prototype for initgraph is

void initgraph(int *graphdriver, int *graphmode, char *pathtodriver);

and a string literal can't be implicitly converted to a non-const char* in standards-compliant C++.

The warning should go away if you replace the literal with an array that can be converted to char*:

char driver[] = "";
initgraph(&gd, &gm, driver);
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • is this `graphics.h` a c++ header? – Iharob Al Asimi Jan 19 '15 at 15:17
  • @iharob That doesn't matter. – molbdnilo Jan 19 '15 at 15:20
  • It was just a question, I am not saying it does matter, I removed my upvote. – Iharob Al Asimi Jan 19 '15 at 15:22
  • It depends on what `initgraph` does with the string. If it doesn't modify the string (and the missing `const` is just historical), then a `const_cast` is a valid solution. If it does modify the string, then you'll need to know the required size of the array you pass it. (Depending on how it is specified, it may also be possible to pass it a null pointer.) – James Kanze Jan 19 '15 at 15:31