472,143 Members | 1,288 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,143 software developers and data experts.

Warnings of deprecated conversion from string to char* while simulating a command line

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::parse(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.

Jul 23 '05 #1
5 7332
Anton Pervukhin wrote:
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::parse(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?
I'd probably go with

char argv0[] = "optionalProgramName";
char argv1[] = "-a";

....

argv[0] = argv0;
argv[1] = argv1;

....
[..]


OTOH, why don't you declare 'parse' to accept an array of pointers to
const char instead? You're not changing them, are you?

V
Jul 23 '05 #2
Anton Pervukhin wrote:

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?


It seems that it is unclear to you on what the warning really refers to.

It talks about a constant string, such as

"optionalProgramName"

such a string has type
const char[]

note the const!

In the assignment the array decays to a pointer as usual, and you are
left with (just showing the data types)

char* = const char*

For this the compiler needs to drop the const on the right hand side of
the assignement. This is legal and was introduced in order to simplify
code migration from C to C++ but it is something you should be aware of
(hence the warning: the characters in such a string *are* constant and
you are not allowed to change them)

You have 2 options:
* either explicitely cast away the constness
* or do the things the right way and define the
argv varaible correctly as:

const char* argv[5];
argv[0] = "optionalProgramName";

Again: note the const!

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3
Anton Pervukhin wrote:

Does somebody know the way to initialize char* with a constant string
that won't give warnings?

The problem is that string literals as you have used them really are
made up of const chars. You are violating const (but the language
grants a special exception for this) to initialize char* with them,
but you can't really modify the strings.

The easiest way around this would be just to allocate writable char
arrays:
char my_arg0[] = "whatever the first arg is";
and pass that to the array.
Jul 23 '05 #4
> char argv0[] = "optionalProgramName";
char argv1[] = "-a";
....
argv[0] = argv0;
argv[1] = argv1;
Thanks! That works.
OTOH, why don't you declare 'parse' to accept an array of pointers to
const char instead? You're not changing them, are you?


No, I'm not changing them.
'parse' is taken from third party, therefore I cannot change it
directly.
I could advice to do it, but one thing prevents me:

if you have your main with lets say CommandLineParser object which has
'parse' function that just repeats the main arguments, like
following...

int main(int argc, char* argv[]) {

CommandLineParser parser;
parser.parse(argc, argv);
...
return ProgramInvoker.getInstance().run(parser.getArgumen t1(),
parser.getArgument2()...);
}

Can I have the declaration for 'parse' like this:

void CommandLineParser::parse(int argc, const char* argv[]); ?

I guess it won't work.

Jul 23 '05 #5
Anton Pervukhin wrote:
[...]
OTOH, why don't you declare 'parse' to accept an array of pointers to
const char instead? You're not changing them, are you?

No, I'm not changing them.
'parse' is taken from third party, therefore I cannot change it
directly.


I was afraid you'd say that... :-)
I could advice to do it, but one thing prevents me:

if you have your main with lets say CommandLineParser object which has
'parse' function that just repeats the main arguments, like
following...

int main(int argc, char* argv[]) {

CommandLineParser parser;
parser.parse(argc, argv);
...
return ProgramInvoker.getInstance().run(parser.getArgumen t1(),
parser.getArgument2()...);
}

Can I have the declaration for 'parse' like this:

void CommandLineParser::parse(int argc, const char* argv[]); ?

I guess it won't work.


No, it won't. Foo** is not convertible to const Foo**. You could try
declaring it as

void CommandLineParser::pars(int, const char* const* argv);

and that should be OK.

V
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by stromhau | last post: by
6 posts views Thread by pete142 | last post: by
2 posts views Thread by David | last post: by
1 post views Thread by Robert Singer | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.