471,851 Members | 986 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 26084
"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**********************************@v53g2000hsa. googlegroups.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.comwrote:
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.comwrote:
>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...
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by cdg | last post: by
11 posts views Thread by santosh | last post: by
6 posts views Thread by Grey Alien | last post: by
NeoPa
reply views Thread by NeoPa | last post: by
aboka
reply views Thread by aboka | 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.