Sunday, August 23, 2009

deprecated conversion warning for char* in GCC

g++-4.x gives following warning whenever a string with in double quotes is assigned to a char * :

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

Though there is no fix available as far as I know except for following two solutions:
1. convert 'char *' to 'const char*' whenever a string is to be assigned.
2. use a type cast to convert a string to a char*.

For example:

const char *name = "Adam Smith"
char *name = (char*) "Adam Smith"


To get rid of deprecated conversion warning, pass -Wno-write-strings option to g++

2 comments:

awhan said...

what a coincidence... i read the following post a few days back

http://learningcppisfun.blogspot.com/2009/07/string-literals-in-c.html

awhan said...

int main()
{
const char * a = "valid" ;
char * b = "invalid" ;

return 0 ;
}

any ways its time we move on and start using real C++ constructs ... i m not using std::string for almost all situations.

give STL a try ... the C++0x standard is just around the corner with many features from boost etc.