PRB: const char* not same as const CPTR where #typedef char* C (123461)
The information in this article applies to:
- Microsoft Visual C++, 32-bit Editions 2.0
- Microsoft Visual C++, 32-bit Editions 2.1
- Microsoft Visual C++, 32-bit Editions 4.0
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 5.0
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
This article was previously published under Q123461 SYMPTOMS
The following program compiles, but one of the following warnings appears:
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();
}
CAUSE
This occurs for two reasons. First, typedefs are not macros, so modifiers
like const or volatile apply to the whole construct. When the following
are used
#typedef char* CPTR
const CPTR cPtr;
they are equivalent to "char* const cPtr;" not "const char* cPtr;."
Second, since "char* const cPtr;" declares cPtr as a constant pointer to
character data and "const char* cPtr;" declares cPtr as a pointer to
constant character data, the two declarations are quite different.
Thus, a "different const qualifier" warning is generated for
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.
REFERENCES
For more information on the const keyword, search the online documentation
provided with Visual C++, 32-bit edition, for "const" or "constant values."
For more information on the typedef keyword, search the online
documentation for "#typedef" or "typedef specifier."
Modification Type: | Minor | Last Reviewed: | 7/5/2005 |
---|
Keywords: | kbCompiler kbprb KB123461 kbAudDeveloper |
---|
|