1 回答
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"}