views:

5105

answers:

4

I have a class with a private char str[256];
and for it i have an explicit constructor,

explicit myClass(const char *func)
{
    strcpy(str,func);
}

i call it as,
myClass obj("example");

when i compile this i get the following warning:
deprecated conversion from string constant to 'char*'

can anyone please provide a pointer to this as to why this is happening?

+1  A: 

In fact a string constant literal is neither a const char * nor a char* but a char[]. Its quite strange but written down in the c++ specifications; If you modify it the behavior is undefined because the compiler may store it in the code segment.

dan ionescu
I would say it is const char[] because as a rvalue you cannot modify it.
fnieto
@Brian, no. See 2.13.4/1 in 03 standard or 2.13.4/6 in 0x draft. "A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration".
fnieto
+3  A: 

Why is this happening? I think the most likely explanation is that you're looking in the wrong place. The code you showed doesn't require the conversion from string literal to char*. The warning must have come from some other place.

sellibitze
A: 

It should be noted that in this context, "deprecated" just means that a function's use is not recommended;

It should also be noted that the secure functions do not prevent or correct security errors; rather, they catch errors when they occur. They perform additional checks for error conditions, and in the case of an error, they invoke an error handler.

For example, the strcpy function has no way of telling if the string that it is copying is too big for its destination buffer. However, its secure counterpart, strcpy_s, takes the size of the buffer as a parameter, so it can determine if a buffer overrun will occur. If you use strcpy_s to copy eleven characters into a ten-character buffer, that is an error on your part; strcpy_s cannot correct your mistake, but it can detect your error and inform you by invoking the invalid parameter handler.

Hope following article will be helpful to you. http://msdn.microsoft.com/en-us/library/8ef0s5kh(VS.80).aspx

Vadakkumpadath
"the secure functions" as such do not exist. It is a non-portable Microsoft-only extension (which arguably is not a stupid idea per se). Using the old str* functions on MS VC++ would lead to a "4996 deprection warning", but he did not mention something along these lines.
gimpf
And, btw, how is this related to the question?
gimpf
If possible, please try this code at desk. Warning will disappear once you changed strcpy to strcpy_s. Please read the article too.
Vadakkumpadath
You have a different warning in mind. OP's warning comes from the unfortunate fact that C and C++ allow you to have a non-const pointer to a string literal: `char* str = "literal"`, even though it is illegal to modify `str`. The warning suggests replacing this with `const char* str = "literal"`, since this would allow it to catch attempts to modify the literal at compile-time. ... Except OP's code appears to be correct in this regard.
UncleBens
@UncleBens - I see no literal mismatch in given code. First the constructor passes "example" which is of type const char*. Constructor accepts the same type. Then two char pointers are passing to strcpy. First is a non const and second is a const. strcpy also accepts the same type. So can you please suggest where could be the deprecate warning? I suggested it as a security issue with proof. Please see the article directly from microsoft which talks about the warning with exactly the same example in given code (strcpy deprecate warning).
Vadakkumpadath
"I see no literal mismatch in given code." That's because the code that actually caused the warning wasn't shown. The cited warning is most likely triggered by a `const char[]` to `char*` conversion.
sbi
+5  A: 

The warning

deprecated conversion from string constant to 'char*'

is given because you are doing somewhere (not in the code you posted) something like:

void foo(char* str);
foo("hello");

the problem is that you are trying to convert a string literal (with type const char[]) to char*.

You can convert a const char[] to const char* because the array decay to the pointer, but what you are doing is to make mutable a constant.

This conversion is probably allowed for C compatibility and just gives you the warning mentioned.

fnieto