Ok, does anyone have a proper solution to deal with warning: deprecated conversion from string constant to 'char*' warnings in G++ code other than suppressing the warning.
Thanks.
Thanks.
This forum is a read-only archive of the Gammon Software forum (2000–2026). No new posts can be made. Search the archive.
Posted by Robert Powell on Sat 24 Oct 2009 04:02 AM — 13 posts, 55,738 views.
void my_func (char * s) { blah }
void my_func (string s) { blah }
#include <iostream>
#include <string>
using namespace std;
void my_func (char * s)
{
cout << s << endl;
}
int main (void)
{
my_func ("nick");
return 0;
}
test1.cpp: In function ‘int main()’:
test1.cpp:14: warning: deprecated conversion from string constant to ‘char*’
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
void my_func (string s)
{
cout << s << endl;
}
int main (void)
{
char s1 [] = "hello there";
char s2 [100];
strcpy (s2, "testing");
my_func ("nick");
my_func (s1);
my_func (s2);
return 0;
}
string s3 = "test";
printf ("%s\n", s3.c_str ());
void my_func (const string & s)
{
cout << s << endl;
}
char buf[MSL];
gcc - GNU project C and C++ compiler
void f(SomeType arg) { ... }
SomeOtherType val;
f(val);
#include <string>
#include <string.h>
using namespace std;
void my_func (const char * s)
{
printf ("I got %s\n", s);
}
void my_func (const string & s)
{
my_func (s.c_str ());
}
int main (void)
{
char s1 [] = "hello there";
char s2 [100];
strcpy (s2, "testing");
my_func ("nick");
my_func (s1);
my_func (s2);
return 0;
}