473,836 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The deprecated conversion from string constant to 'char*'

I am working on a large, old code base and attempting to move it to
GCC 4.2. Throughout the code, there is stuff like:

char *aVar = "aString";

or

void aFunc( char *aVar) { ... }
aFunc( "aString" );

With this latest version of GCC, such code now generates the warning:

warning: deprecated conversion from string constant to 'char*'

and since I hate warnings, I am wondering what the best way to handle
this situation is.

There are, of course, some warnings which can safely be disabled and
ignored...is this one of them?
It doesn't seem to be based on the fact that it is 'deprecated' which
I interpret as meaning the ability to do this in the future will go
away.

Now, I can get rid of the warning by placing a (char*) in front of the
string (but not (const char*)...why?).
I can also get rid of the warning by changing the code to look like:

const char *aVar = "aString";
void aFunc( const char *aVar) { ... }

which seems to be a better solution.

Any thoughts, comments or suggestions?

Oct 3 '08 #1
9 26362
"Eric" <er******@gmail .comwrote:
>I am working on a large, old code base and attempting to move it to
GCC 4.2. Throughout the code, there is stuff like:

char *aVar = "aString";

or

void aFunc( char *aVar) { ... }
aFunc( "aString" );

With this latest version of GCC, such code now generates the warning:

warning: deprecated conversion from string constant to 'char*'

and since I hate warnings, I am wondering what the best way to handle
this situation is.

There are, of course, some warnings which can safely be disabled and
ignored...is this one of them?
It doesn't seem to be based on the fact that it is 'deprecated' which
I interpret as meaning the ability to do this in the future will go
away.

Now, I can get rid of the warning by placing a (char*) in front of the
string (but not (const char*)...why?).
I can also get rid of the warning by changing the code to look like:

const char *aVar = "aString";
void aFunc( const char *aVar) { ... }

which seems to be a better solution.

Any thoughts, comments or suggestions?
"Casting away" a warning is hardly ever a good choice.
You should change your "char*" to "const char*".
If a statement really changes your string literal, it is most likely
unintentional.. .
Oct 3 '08 #2
Use either const char* foo="bar"; or char foo[] = "bar";, depending on
whether you want to modify bar or not.
Oct 3 '08 #3
Eric schrieb:
I am working on a large, old code base and attempting to move it to
GCC 4.2. Throughout the code, there is stuff like:

char *aVar = "aString";

or

void aFunc( char *aVar) { ... }
aFunc( "aString" );

With this latest version of GCC, such code now generates the warning:

warning: deprecated conversion from string constant to 'char*'

and since I hate warnings, I am wondering what the best way to handle
this situation is.

There are, of course, some warnings which can safely be disabled and
ignored...is this one of them?
It doesn't seem to be based on the fact that it is 'deprecated' which
I interpret as meaning the ability to do this in the future will go
away.

Now, I can get rid of the warning by placing a (char*) in front of the
string (but not (const char*)...why?).
I can also get rid of the warning by changing the code to look like:

const char *aVar = "aString";
void aFunc( const char *aVar) { ... }

which seems to be a better solution.

Any thoughts, comments or suggestions?
#include <string>

const std::string aVar( "aString" );

void aFunc( const std::string& val ) { ... }
Don't use char* if you can use std::string. You will avoid many segfaults.
Lars
Oct 3 '08 #4
char *aVar = "aString";
void aFunc( char *aVar) { ... }
aFunc( "aString" );
It's important to realize *why* this is dangerous.

Consider:

void aFunc(char *s) { s[0] = 'X'; }
...
char *aVar = "aString";
char *aVar2 = "aString";
aFunc( aVar );
cout << aVar; // prints "XString"
cout << aVar2; // *also* prints "XString"

A compiler is completely free to only store a given
c-string literal in a single location (most do).
It's also free to store it in read-only memory.

Bottom line: modifying a string literal is
a bug, and requiring that pointers to string
literals are const is a compiler's only chance
of making sure you don't do it.

Sean
Oct 3 '08 #5
In message
<ff************ *************** *******@v53g200 0hsa.googlegrou ps.com>,
Eric <er******@gmail .comwrites
>I am working on a large, old code base and attempting to move it to
GCC 4.2. Throughout the code, there is stuff like:

char *aVar = "aString";

or

void aFunc( char *aVar) { ... }
aFunc( "aString" );

With this latest version of GCC, such code now generates the warning:

warning: deprecated conversion from string constant to 'char*'

and since I hate warnings, I am wondering what the best way to handle
this situation is.

There are, of course, some warnings which can safely be disabled and
ignored...is this one of them?
It doesn't seem to be based on the fact that it is 'deprecated' which
I interpret as meaning the ability to do this in the future will go
away.

Now, I can get rid of the warning by placing a (char*) in front of the
string (but not (const char*)...why?).
I can also get rid of the warning by changing the code to look like:

const char *aVar = "aString";
void aFunc( const char *aVar) { ... }

which seems to be a better solution.

Any thoughts, comments or suggestions?
Definitely the latter. That way you are telling the compiler the truth,
which is that you don't wish to modify the thing pointed to. That's far
better than lying to it (or just saying that you know better), which is
usually what casting away constness amounts to.

--
Richard Herring
Oct 3 '08 #6
On 2008-10-03 09:19:08 -0400, Eric <er******@gmail .comsaid:
I am working on a large, old code base and attempting to move it to
GCC 4.2. Throughout the code, there is stuff like:

char *aVar = "aString";

or

void aFunc( char *aVar) { ... }
aFunc( "aString" );

With this latest version of GCC, such code now generates the warning:

warning: deprecated conversion from string constant to 'char*'

and since I hate warnings, I am wondering what the best way to handle
this situation is.

There are, of course, some warnings which can safely be disabled and
ignored...is this one of them?
From the perspective of the language, certainly.
It doesn't seem to be based on the fact that it is 'deprecated' which
I interpret as meaning the ability to do this in the future will go
away.
Deprectaed means that it might go away.
>
Now, I can get rid of the warning by placing a (char*) in front of the
string (but not (const char*)...why?).
I can also get rid of the warning by changing the code to look like:

const char *aVar = "aString";
void aFunc( const char *aVar) { ... }

which seems to be a better solution.

Any thoughts, comments or suggestions?
It depends. If it's just the warning that concerns you, turn it off.
That's much safer than making pervasive changes to working code.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 3 '08 #7
Eric wrote:
I am working on a large, old code base and attempting to move it to
GCC 4.2. Throughout the code, there is stuff like:

char *aVar = "aString";

or

void aFunc( char *aVar) { ... }
aFunc( "aString" );

With this latest version of GCC, such code now generates the warning:

warning: deprecated conversion from string constant to 'char*'

and since I hate warnings, I am wondering what the best way to handle
this situation is.

There are, of course, some warnings which can safely be disabled and
ignored...is this one of them?
It doesn't seem to be based on the fact that it is 'deprecated' which
I interpret as meaning the ability to do this in the future will go
away.
I'm not sure I parsed the above sentence correctly. It is deprecated
because it is unsafe, so the warning probably exists for both reasons
:-)

But it isn't going away: it's proliferating! The current working draft
has an analogous one for u/U-prefixed string literals.
Now, I can get rid of the warning by placing a (char*) in front of the
string (but not (const char*)...why?).
Because the conversion only exists from a string literal expression: if
the expression is (const char *) "abc", instead, it is not a string
literal, and has type const char *, which doesn't convert implicitly to
char *.
I can also get rid of the warning by changing the code to look like:

const char *aVar = "aString";
void aFunc( const char *aVar) { ... }

which seems to be a better solution.
I'd recommend the modification, yes (a common problem when working with
old code is that, for various reasons, you cannot modify it. But you
can, so... be happy :-))

In the latter example, above, I'd write

const char aVar[] = "aString" ;

but that's a minor issue.

--
Gennaro Prota | name.surname yahoo.com
Breeze C++ (preview): <https://sourceforge.net/projects/breeze/>
Do you need expertise in C++? I'm available.
Oct 3 '08 #8
On Oct 3, 1:15*pm, Gennaro Prota <gennaro/pr...@yahoo.com wrote:
Eric wrote:
I am working on a large, old code base and attempting to move it to
GCC 4.2. Throughout the code, there is stuff like:
* char *aVar = "aString";
or
* void aFunc( char *aVar) { ... }
* aFunc( "aString" );
With this latest version of GCC, such code now generates the warning:
* warning: deprecated conversion from string constant to 'char*'
and since I hate warnings, I am wondering what the best way to handle
this situation is.
There are, of course, some warnings which can safely be disabled and
ignored...is this one of them?
It doesn't seem to be based on the fact that it is 'deprecated' which
I interpret as meaning the ability to do this in the future will go
away.

I'm not sure I parsed the above sentence correctly. It is deprecated
because it is unsafe, so the warning probably exists for both reasons
:-)

But it isn't going away: it's proliferating! The current working draft
has an analogous one for u/U-prefixed string literals.
Well, that would appear to be bad...
Oct 3 '08 #9
On 2008-10-03 14:06:22 -0400, Eric <er******@gmail .comsaid:
On Oct 3, 1:15¬*pm, Gennaro Prota <gennaro/pr...@yahoo.com wrote:
>Eric wrote:
>>I am working on a large, old code base and attempting to move it to
GCC 4.2. Throughout the code, there is stuff like:
>>¬* char *aVar = "aString";
>>or
>>¬* void aFunc( char *aVar) { ... }
¬* aFunc( "aString" );
>>With this latest version of GCC, such code now generates the warning:
>>¬* warning: deprecated conversion from string constant to 'char*'
>>and since I hate warnings, I am wondering what the best way to handle
this situation is.
>>There are, of course, some warnings which can safely be disabled and
ignored...i s this one of them?
It doesn't seem to be based on the fact that it is 'deprecated' which
I interpret as meaning the ability to do this in the future will go
away.

I'm not sure I parsed the above sentence correctly. It is deprecated
because it is unsafe, so the warning probably exists for both reasons
:-)

But it isn't going away: it's proliferating! The current working draft
has an analogous one for u/U-prefixed string literals.

Well, that would appear to be bad...
Or good, depending on what you think is important.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Oct 3 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
7367
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:
24
11042
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include <string> #include <vector> #include <cstring> // presume s from somewhere, such as:
5
7435
by: Anton Pervukhin | last post by:
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
5
2698
by: cdg | last post by:
Could anyone explain how to write this sample program correctly. I need to convert an integer to a string. However, I would prefer to convert the integer to char array. But I didn`t want to use "itoa". And I am not that familar with using "stringstream". And is there better approach to converting integers to char arrays. #include <iostream> #include <sstream> #include <string> using namespace std;
11
3171
by: santosh | last post by:
Hello all, Conversion macros along the name of INT8_C, INT16_C etc, are defined in stdint.h to convert their argument into suitable representations for their corresponding types, i.e. int8_t, int16_t etc. My questions are: 1. Should the conversion macros be used even when assigning from a variable of the same type?
11
2713
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h> #include <string.h> #define LENGTH 20 int temp, m, n, i, r, base10, true;
6
2355
by: Peter Lee | last post by:
what's the correct behaver about the following code ? ( C++ standard ) I got a very strange result.... class MyClass { public: MyClass(const char* p) { printf("ctor p=%s\n", p);
5
4007
by: kelvin.koogan | last post by:
I want to declare an array of constant strings. In unmanaged C++ I'd do this static const char *string = { "One", "Two, "Three" }; However if I do this in .NET and want to pass these to a function which takes an Object * then I get compilation errors. The only way to avoid these seems to be to create a temporary string on the heap, e.g.
6
1825
by: Grey Alien | last post by:
I am baffled by this behaviour. I have a class A declared as follows: class A { public: A(); explicit A(const std::string& value); explicit A(const TimestampParam& value) ;
0
9656
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10819
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10526
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9349
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
7771
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
5641
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...
0
5811
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3100
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.