Alex Rivera | Logout

How to get rid of `deprecated conversion from string constant to ‘char*’` warnings in GCC

Asked 2008-09-12T18:15:55.320
429

I'm working on an exceedingly large codebase, and recently upgraded to GCC 4.3, which now triggers this warning:

warning: deprecated conversion from string constant to ‘char*’

Obviously, the correct way to fix this is to find every declaration like

char *s = "constant string";

or function call like:

void foo(char *s);
foo("constant string");

and make them const char pointers. However, that would mean touching 564 files, minimum, which is not a task I wish to perform at this point in time. The problem right now is that I'm running with -Werror, so I need some way to stifle these warnings. How can I do that?

Edit
Report

3 Answers

75

I had a similar problem, and I solved it like this:

#include <string.h>

extern void foo(char* m);
 
int main() {
    // warning: deprecated conversion from string constant to ‘char*’
    //foo("Hello");
   
    // no more warning
    char msg[] = "Hello";
    foo(msg);
}

I did not have access to foo in order to adapt it to accept const char*, which would be a better solution because foo did not change m.

answered 2009-02-12T12:44:01.997
25

I can't use the compiler switch. So I have turned this:

char *setf = tigetstr("setf");

to this:

char *setf = tigetstr((char *)"setf");
answered 2009-08-21T02:14:26.480
-1

See this situation:

typedef struct tagPyTypeObject
{
    PyObject_HEAD;
    char *name;
    PrintFun print;
    AddFun add;
    HashFun hash;
} PyTypeObject;

PyTypeObject PyDict_Type=
{
    PyObject_HEAD_INIT(&PyType_Type),
    "dict",
    dict_print,
    0,
    0
};

Watch the name field. Using gcc, it compiles without warning, but in g++ it will. I don't know why.

answered 2010-08-24T02:14:08.547

Your Answer