Waleed A
Waleed A

Reputation: 105

Disable warning "deprecated conversion from string constant to 'char*' [-Wwrite-strings]"

I have these two lines in my code:

RFM2G_STATUS   result;   
result = RFM2gOpen( "\\\\.\\rfm2g1", &rH );

I get the error message:

"warning: deprecated conversion from string constant to 'char*' [-Wwrite-strings]
     result = RFM2gOpen( "\\\\.\\rfm2g1", &rH );"

Actually I can not modify it to

const RFM2G_STATUS   result;   

because RFM2G_STATUS is pre-defined in another file and does not accept const before. Is there another way to disable this warning message?

Upvotes: 1

Views: 2133

Answers (4)

Baum mit Augen
Baum mit Augen

Reputation: 50063

You seem to be absolutely sure that RFM2gOpen does not modify the input string, otherwise you would have undefined behavior in your code as it stands now.

If you are sure that the input data will not be written to, you can const_cast the constness away safely:

result = RFM2gOpen(const_cast<char*>("\\\\.\\rfm2g1"), &rH );

Again, this is only safe if the routine does not write to the input string, ever, otherwise this is undefined behavior!

If you are not completely sure that this method will never write to the character array, copy the string to an std::vector<char> and pass the .data() pointer to the function (or use a simple char array as Bo Persson suggests, that would most likely be more efficient/appropriate than the vector).

Upvotes: 1

Shadocko
Shadocko

Reputation: 1216

It would seem that the function RFM2gOpen() expects a non-const char* as first parameter (see here), as it can sometimes happen with legacy API's (or API's written by lazy coders), and string litterals are of type const char* so a deprecated implicit conversion is happening (getting rid of the const qualifier).

If you're *100% sure that the function won't modify the pointed-to memory, then and only then can you just put an explicit conversion, e.g. const_cast<char*>("\\\\.\\rfm2g1") or (C-style) (const char*)"\\\\.\\rfm2g1"

Upvotes: 0

Ross Bencina
Ross Bencina

Reputation: 4173

One possible fix is:

RFM2gOpen(const_cast<char*>("\\.\rfm2g1"), &rH);

This may cause a runtime fault if RFM2gOpen tries to modify the string.

The following is less likely to cause a memory fault, but it still undefined behavior:

std::string s("\\.\rfm2g1");
RFM2gOpen(const_cast<char*>(s.c_str()), &rH);

To be fully conformant you need to copy the "\.\rfm2g1" to a mutable buffer. Something like:

char *s = alloca(strlen("\\.\rfm2g1")+1);
strcpy(s, "\\.\rfm2g1");
RFM2gOpen(s, &rH);

The real fix, of course, is for RFM2gOpen to be updated to take a const char*.

Upvotes: 1

Bo Persson
Bo Persson

Reputation: 92271

Like the message says, conversion from const char* to char* (which C++ inherited from ancient C language which didn't have const) has been deprecated.

To avoid this, you can store the parameter in a non-const string, and pass that to the function:

char parameter[] = "\\\\.\\rfm2g1";
RFM2G_STATUS   result;   
result = RFM2gOpen( parameter, &rH );

That way you avoid the ugly casts.

Upvotes: 2

Related Questions