Dear all,
I did the following analysis to conclude that the following pointer
types are not compatible. Please let me know If my analysis and
interpretation of the C standard are correct:
const char * : "pointer to const-qualified char".
char *: "pointer to char".
Are these pointed-to types compatibles?
--------------------------
C89
--------------------------
6.5.4.1
Pointer declarators
....
For two pointer types to be compatible, both shall be identically
qualified
and both shall be pointers to compatible types.
....
--------------------------
Well, using definition in 6.5.4.1 we can see that we comply with the
first part
("both shall be identically qualified").
Both are identically qualified (no qualifiers, both are unqualified
types).
Let us see now about the second part of definition in 6.5.4.1
("and both shall be pointers to compatible types").
So do these pointers:
1) const char *; /* pointer to const-qualified char */
2) char *; /* pointer to char */
point to compatible types?
In the case of pointer 1) const char *;
this pointer points to a 'const-qualified char'.
In the case of pointer 2) char *;
this pointer points to a 'char'.
Is a 'const-qualified char' compatible with a
'char'?
--------------------------
C89
--------------------------
6.1.2.5
Types
.....
The qualified or unqualified versions of a type are distinct types that
belong to the same type category and have the same representation and
alignment requirements.
....
--------------------------
As we can see according to 6.1.2.5 and
6.1.2.6 ("Two types have compatible type if their types are the same").
A 'const-qualified char' is NOT compatible with a 'char'
because their types are different.
And as we can see:
1) const char *; /* pointer to const-qualified char */
2) char *; /* pointer to char */
cannot satisfy second part of definition in
6.5.4.1 ("and both shall be pointers to compatible types"). Because
they do not point to compatible types.
Therefore 1) and 2) are NOT compatible pointers.
Thank you very much to everybody in advance,
Max