Article ID: 123461
Article Last Modified on 7/5/2005
C4090: 'initializing' : different 'const' qualifiers:-or-
C2440: 'initializing' : cannot convert from 'const char *' to 'char *'
Conversion loses qualifiers
const char *ReturnConstantPtr()
{
const char *p = "can't touch this";
return p;
}
void main()
{
char *p = ReturnConstantPtr();
}
However, the above program will compile without exception if a type
definition is used for char* as follows:
typedef char* CPTR;
const CPTR ReturnConstantPtr()
{
const CPTR p = "can't touch this";
return p;
}
void main()
{
CPTR p = ReturnConstantPtr();
}
#typedef char* CPTR const CPTR cPtr;they are equivalent to "char* const cPtr;" not "const char* cPtr;."
char *p = ReturnConstantPtr();because it is trying to set p equal to a pointer that points to a constant character whose value *p could try to change. Alternatively, no warning is generated for
CPTR p = ReturnConstantPtr();because it is setting p equal to a constant pointer that points to a character whose value *p may freely change.
Additional query words: 9.00
Keywords: kbprb kbcompiler KB123461