3

Possible Duplicate:
C++ deprecated conversion from string constant to ‘char*’

I am having following code, though i didn't copy full code because it is huge. Following code is in template class, and i am getting warning as below. Because of warning in template i am not able to instantiate it and getting "instantiated from here" error.

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

void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, char* pcMessage, char* pcFileName, unsigned int RowNo)
{
//...
}


char cCompleteMessage[200];
memset(cCompleteMessage, 0x00, sizeof(cCompleteMessage));
char*cMessage = "add reorgenize failed";
ErrorMessageInRaphsodyCode(cCompleteMessage, cMessage, "omcollec.h", __LINE__);

My question is what is best way to get rid of above warning ?

4
  • 1
    You want to declare cMessage as const char* Commented Sep 14, 2011 at 11:10
  • Isn't depreciation something that happens to financial assets? ;) Commented Sep 14, 2011 at 11:14
  • 1
    Please read stackoverflow.com/questions/1449480/… Commented Sep 14, 2011 at 11:44
  • And if you simply want to ignore the error (it's your life, not mine) stackoverflow.com/questions/59670/… Commented Sep 14, 2011 at 11:48

5 Answers 5

11

If a function takes a char const *, it guarantees that it only reads whatever data the pointer points to. However, if it takes a non-const pointer, like char *, it might write to it.

As it is not legal to write to a string literal, the compiler will issue a warning.

The best solution is to change the function to accept char const * rather than char *.

Sign up to request clarification or add additional context in comments.

2 Comments

problem i am facing is i cannot change the function declaration, it is provided by library, and it takes char*, and i want to pass argument "myarg", how can i pass it
You can silence the warning by casting the pointer to char *. You can do this directly on the string literal, or by adding a wrapper function, as suggested by others.
7
char cMessage[] = "add reorganize failed";

This should get rid of the warning.

12 Comments

This will create a character array, located in RAM, which would occupy precious resources and consume time being initialized.
@Lindydancer If the function requires a char*, it requires a char*. What alternatives are there? (other than changing the function signature)
@Lindydancer: regarding precious resources, 22 bytes of RAM costs about 2*10^-5 of a penny. If there's any danger at all that the function takes a non-const char* because it modifies the data, then copying the string literal is essential. Slow, correct code is better than fast code that attempts to modify a string literal.
@Lindydancer: so what's your alternative, given that the function can't be changed and we don't know what it does? This answer is right. If there's more information available that could save 22 bytes (or 24 once it's padded) of stack, and the time and code to copy that much data, then great. Someone programming in a restricted environment might think it's worth checking that out to ensure the function doesn't modify the data. But let's not over-sell this by calling that amount of time and memory "precious" when it almost always isn't.
@Lindydancer: I would note that the message can easily be made static, to avoid the stack waste etc... no assumption should be made on the function body, the signature is the only contract.
|
2

Best way to get rid of it is to fix the function that is taking the parameter.

If your code is correct and the function does indeed take string constants, it should say so in its prototype:

void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, char* pcMessage, const char* pcFileName, unsigned int RowNo)

If you can't do that (you don't have the code), you can create an inline wrapper:

inline void ErrorMessageInRaphsodyCodeX(char* p1, char* p2, const char* p3, unsigned int p4)
{  ErrorMessageInRaphsodyCode(p1,p2,(char*)p3,p4); }

and use the wrapper instead.

If your code is incorrect and the function does actually require writeable memory (which I highly doubt), you will need to make the string writeable by either creating a local array as Jan suggested, or mallocating enough memory.

Comments

0

(1) Make the variable a const char*

(..., const char* pcFileName, ...)

(2) If above is not possible and you want to retain the state of char* and const char* then make the function a template:

template<typename CHAR_TYPE>  // <--- accepts 'char*' or 'const char*'
void ErrorMessageInRaphsodyCode(char* pcCompleteMessage, CHAR_TYPE* pcMessage, char* pcFileName, unsigned int RowNo)
{
//...
}

Comments

0

function c_str() of std::string class.

2 Comments

There is no std::string:s anywhere in this code, how could it possibly change anything (apart form making the application larger and slower).
This does not really qualify as a complete answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.