473,883 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
+ 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(c har **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] = "optionalProgra mName";
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 7438
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(c har **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] = "optionalProgra mName";
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[] = "optionalProgra mName";
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] = "optionalProgra mName";
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

"optionalProgra mName"

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] = "optionalProgra mName";

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[] = "optionalProgra mName";
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 CommandLinePars er object which has
'parse' function that just repeats the main arguments, like
following...

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

CommandLinePars er parser;
parser.parse(ar gc, argv);
...
return ProgramInvoker. getInstance().r un(parser.getAr gument1(),
parser.getArgum ent2()...);
}

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

void CommandLinePars er::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 CommandLinePars er object which has
'parse' function that just repeats the main arguments, like
following...

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

CommandLinePars er parser;
parser.parse(ar gc, argv);
...
return ProgramInvoker. getInstance().r un(parser.getAr gument1(),
parser.getArgum ent2()...);
}

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

void CommandLinePars er::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 CommandLinePars er::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
7372
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 `char *' In Line 19 of my file 'robot.cpp' I declare the constructor of class robot like:
2
11200
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
4898
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 -Wall -Wunused -Werror -W -Wmissing-prototypes -Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -c -o foo.o foo.c
1
2718
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 something to do with the inclusions of header files. Here are the files i include in main.cpp file #include "SDL.h" // SDL: window & input library #include "SDL_opengl.h" // platform independent OpenGL include #include...
2
3927
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... 1>Polygonstruct.cpp 1>C:\Program Files\Microsoft Visual Studio 8\VC\include\xlocnum(590) : warning C4312: 'type cast' : conversion from 'uintptr_t' to 'void *' of greater size 1> C:\Program Files\Microsoft Visual Studio 8\VC\include\xlocnum(566) :...
6
5155
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 to_char(TRADE_DATE,'yyyymmdd') trade_date , THEORETICAL_HIGH_VALUE" " from %s " " where trade_date = (select max(trade_date ) from %s where " <snip>
6
1916
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
1390
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 first thing I notice is that I am not getting any warnings, even though the Page.RegisterStartupScript calls are still there. I know about those, so I can find and fix. But there could be other usages of deprecated methods that I donlt know...
1
2522
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 python 2.5.2. (although it is made for older version of python) and cannot find a way to get rid of this error (code and errors below). Does anyone know how to avoid it ? I would appreciate all help and
0
9940
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10415
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9573
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7971
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7126
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5797
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4611
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4220
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3232
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.