Warning: Invalid Conversion From 'char**' To 'const char**'
This page answers questions like these:
- Why is an implicit cast from char** to const char** illegal?
- Why can’t I pass char** to const char**?
- What is wrong with casting/converting char** to const char**?
Related Links:
Difference Between char* and char [] and char s[n]
Difference Between char* s = "string"; and char s[] = "string";
warning/error: inline function used but never defined
error: expected primary-expression before ')' token
error: the compiler can assume that the address of '...' will always evaluate to 'true'
Static Member Function Declaration and Definition
warning/error: invalid conversion from 'char**' to 'const char**'
void func2(const char** ptr2)
{
...
}
void func1()
{
char** ptr1;
const char** q = ptr1; // <-- Gives a compilation warning or error!
func2(ptr1); // <-- Gives a compilation warning or error!
}
- In both C++ and C, an implicit conversion from “char**” to “const char**” is illegal.
- In fact, such a conversion is illegal, no matter whether it’s a fundamental type (like char, int, or unsigned long) or a user-defined type (like a class or struct).
- The reason for this warning/error is that such a conversion would allow you to later accidentally (or deliberately) modify a const object.
- In C++, the solution is to use “const char* const *” or “const char* const * const” as the to-type instead of “const char**”.
- In C, the solution is the same as C++, but an explicit cast is also needed.
An Example Of Why An Implicit Cast From 'char**' To 'const char**' Is Illegal:
void func()
{
const TYPE c; // Define 'c' to be a constant of type 'TYPE'.
TYPE* p; // Define 'p' to be a non-constant pointer to a variable of type 'TYPE'.
const TYPE** cpp; // Define 'cpp' to be a non-constant pointer to a non-constant pointer to a constant of type 'TYPE'.
cpp = &p; // Illegal! Assign the address of 'p' (i.e. TYPE**) to 'cpp' (i.e. const TYPE**).
// This implicit cast is disallowed to prevent you writing the next line of code.
*cpp = &c; // Make '*cpp' (i.e. 'p') point to 'c'. This is legal since both are of type 'const TYPE*' .
// (If 'cpp' was of type 'const TYPE* const *', this assignment would be illegal.)
*p = something; // Modify what 'p' points to! i.e. This would modify the constant 'c'!
// This would happen without an explicit cast anywhere in the code which is clearly very dangerous!
}
- After reading the above, you may think to yourself, 'I would never deliberately do that!', but the purpose of the warning/error is to prevent you doing it accidentally!
Related Links:
Difference Between char* and char [] and char s[n]
Difference Between char* s = "string"; and char s[] = "string";
warning/error: inline function used but never defined
error: expected primary-expression before ')' token
error: the compiler can assume that the address of '...' will always evaluate to 'true'
Static Member Function Declaration and Definition
Home > C++ / C > Warning: Invalid Conversion From 'char**' To 'const char**'
Tags: warning invalid conversion from char** to const char**, error invalid conversion from char** to const char**, invalid conversion from char** to const char**, invalid conversion, warning: invalid conversion from 'char**' to 'const char**', error: invalid conversion from 'char**' to 'const char**', warning, error, C++, C
Copyright © HelpDoco.com
code-cpp-c-warning-invalid-conversion-from-ptr-ptr-to-const-ptr-ptr.txt
C++-C/warning-invalid-conversion-from-char-pointer-pointer-to-const-char-pointer-pointer.htm
2