A
Anton Pervukhin
Hello!
Imagine the situation you have a class that interprets the command line
of the program you are executing. This class is written by third party
and has a main function parse with arguments the same as main:
ClassA:arse(char **argv, int argc)
While trying to write test cases for this function I came across the
warnings appeared when I tried to initialize 'char** argv' straightly
from strings, like this:
argv[0] = "optionalProgramName";
argv[1] = "-a"; // some value argument
argv[2] = "aFileName.txt"; // its concrete value
argv[3] = "-nbc"; // some switch arguments
....
but that results in to warning: deprecated conversion from string
constant to `char*'. I'm using gcc 3.4.3 with strict warning policy: -W
-Wall -Wwrite-strings
Does somebody know the way to initialize char* with a constant string
that won't give warnings?
Several ways to do it for conversion to const char* is given in
Josuttis book for standart template library:
one could use std::string functions data(), or c_str() as following:
std::string s("1234");
const char* p;
p = s.c_str(); or
p = s.data();
but that is for const char* and doesn't work directly for char*. Should
one try to convert by putting const away, or there are easier ways to
do it?
The problem is not dramatic but it would be nice to have it without
warnings.
Any ideas will be appreciated.
Thanks.
Imagine the situation you have a class that interprets the command line
of the program you are executing. This class is written by third party
and has a main function parse with arguments the same as main:
ClassA:arse(char **argv, int argc)
While trying to write test cases for this function I came across the
warnings appeared when I tried to initialize 'char** argv' straightly
from strings, like this:
argv[0] = "optionalProgramName";
argv[1] = "-a"; // some value argument
argv[2] = "aFileName.txt"; // its concrete value
argv[3] = "-nbc"; // some switch arguments
....
but that results in to warning: deprecated conversion from string
constant to `char*'. I'm using gcc 3.4.3 with strict warning policy: -W
-Wall -Wwrite-strings
Does somebody know the way to initialize char* with a constant string
that won't give warnings?
Several ways to do it for conversion to const char* is given in
Josuttis book for standart template library:
one could use std::string functions data(), or c_str() as following:
std::string s("1234");
const char* p;
p = s.c_str(); or
p = s.data();
but that is for const char* and doesn't work directly for char*. Should
one try to convert by putting const away, or there are easier ways to
do it?
The problem is not dramatic but it would be nice to have it without
warnings.
Any ideas will be appreciated.
Thanks.