473,473 Members | 1,837 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 7403
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: G. Peter | last post by:
Hi there, I've a 'funny' error message of my compiler (g++ 2.95.4) that tells me: robot.cpp: In method `Robot::Robot()': robot.cpp:19: warning: deprecated conversion from string constant to...
2
by: AMT2K5 | last post by:
Hello. When I compile my program I recieve lots and lots of the following message which I am trying to decipher. "xxx was declared deprecated". What exactly does that mean?
12
by: Charlie Zender | last post by:
Hi, I am unable to compile a large body of code with extremely pedantic compile time checks activate, so that warnings cause errors. With GCC 3.3.1, I do this with gcc -std=c99 -pedantic...
1
by: stromhau | last post by:
Ok, i have a file with main and an additional .cpp file i include in the main file but i get a lot of strange warnings when including. Both files compile just great separately. It seems that it have...
2
by: stromhau | last post by:
When compiling i get these warnings and i dont know why. Can anyone please explain ? Tommy, 1>------ Build started: Project: sdl, Configuration: Release Win32 ------ 1>Compiling......
6
by: tropos | last post by:
For my sins, I'm maintaining some old C code which is migrated to C++. Dozens of lines of it looks like this: char *cfd_THE_ts_result_sql= "select TRADE_DATE , VALUE , " " from (" " select...
6
by: pete142 | last post by:
When I compile this code: typedef unsigned char BYTE; BYTE * IpString(unsigned int ip) { static BYTE ipString; ipString = (BYTE) 0xff & (ip >24); ipString = (BYTE) 0xff & (ip >16);
2
by: David | last post by:
When I did some trial conversions of a .Net 1.1 c# web app earlier this year I got the expected Deprecation warnings, e.g. for Page.RegisterStartupScript. Now I am doing the real conversion and the...
1
by: Robert Singer | last post by:
Platform: winXP, excel 2003 Python 2.5.2 XLWriter 0.4a3 (http://sourceforge.net/projects/pyxlwriter/) Is anyone here using this very nice package, for writing excel files? I'm using it on...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.