Why Is Conversion from String Constant to 'Char*' Valid in C But Invalid in C++

Why is conversion from string constant to 'char*' valid in C but invalid in C++

Up through C++03, your first example was valid, but used a deprecated implicit conversion--a string literal should be treated as being of type char const *, since you can't modify its contents (without causing undefined behavior).

As of C++11, the implicit conversion that had been deprecated was officially removed, so code that depends on it (like your first example) should no longer compile.

You've noted one way to allow the code to compile: although the implicit conversion has been removed, an explicit conversion still works, so you can add a cast. I would not, however, consider this "fixing" the code.

Truly fixing the code requires changing the type of the pointer to the correct type:

char const *p = "abc"; // valid and safe in either C or C++.

As to why it was allowed in C++ (and still is in C): simply because there's a lot of existing code that depends on that implicit conversion, and breaking that code (at least without some official warning) apparently seemed to the standard committees like a bad idea.

error: ISO C++ forbids converting a string constant to ‘char*’

As the comments indicate, you need to use const char*, not char*. It looks like the book is badly out of date.

In C++, a string literal is of type const char[N], where N is the number of characters in the literal plus one for the terminating '\0'.

I was able to make your code compile and run by making the following changed:

  • Change the constructor for String from

    String(char *s)

    to

    String(const char *s)
  • Change the declaration of first from

    char *first = "Joseph ";

    to

    const char *first = "Joseph ";
  • Remove the extraneous semicolon at the end of String::join.

Also, the code would be a lot more legible with proper indentation.

How to handle C char* defines in C++

The problem is that string literals "this is a string literal" are of type char[] in C but const char[] in C++. So if you have sloppily written C code which doesn't use const correctness of function parameters, that code will break when ported to C++. Because you can't pass a const char* to a function expecting char*.

And no, it is generally not safe to "cast away" const - doing so is undefined behavior in C and C++ both.

The solution is to fix the original C code. Not using const correctness is incorrect design, both in C and C++. If the C++ compiler supports compound literals, then one possible fix could also be:

#define IPADDRESS (char[]){"fd9e:21a7:a92c:2323::1"}

C++ deprecated conversion from string constant to 'char*'

This is an error message you see whenever you have a situation like the following:

char* pointer_to_nonconst = "string literal";

Why? Well, C and C++ differ in the type of the string literal. In C the type is array of char and in C++ it is constant array of char. In any case, you are not allowed to change the characters of the string literal, so the const in C++ is not really a restriction but more of a type safety thing. A conversion from const char* to char* is generally not possible without an explicit cast for safety reasons. But for backwards compatibility with C the language C++ still allows assigning a string literal to a char* and gives you a warning about this conversion being deprecated.

So, somewhere you are missing one or more consts in your program for const correctness. But the code you showed to us is not the problem as it does not do this kind of deprecated conversion. The warning must have come from some other place.



Related Topics



Leave a reply



Submit