Expert C Programming

(Jeff_L) #1

Does Section 6.3.16.1 also make a call with a char ** argument corresponding to a const


char ** parameter legal? It does not.


The Examples portion of Section 6.1.2.5 states:


The type designated "const float *" is not a qualified type—its type is "pointer to const-qualified float"
and is a pointer to a qualified type.


Analogously, const char ** denotes a pointer to an unqualified type. Its type is a pointer to a


pointer to a qualified type.


Since the types char and const char are both pointers to unqualified types that are


not the same type, they are not compatible types. Therefore, a call with an argument of type char


corresponding to a parameter of type const char is not allowed. Therefore, the


constraint given in Section 6.3.2.2 is violated, and a diagnostic message must be produced.


This is a subtle point to grasp. Another way of looking at it is to note that:



  • the left operand has type FOO2—a pointer to FOO, where FOO is an unqualified pointer to a
    character qualified by the const qualifier, and

  • the right operand has type BAZ2—a pointer to BAZ, where BAZ is an unqualified pointer to
    a character with no qualifiers.


FOO and BAZ are compatible types, but FOO2 and BAZ2 differ other than in qualifica-tion of the
thing immediately pointed to and are therefore not compatible types; therefore the left and right
operands are unqualified pointers to types that are not compatible. Compatibility of pointer types is
not transitive. Therefore, the assignment or function call is not permitted. However, note that the
restriction serves mainly to annoy and confuse users. The assignment is currently allowed in C++
translators based on cfront (though that might change).


Handy Heuristic


Const Isn't


The keyword const doesn't turn a variable into a constant! A symbol with the const


qualifier merely means that the symbol cannot be used for assignment. This makes the value
re ad -onl y through that symbol; it does not prevent the value from being modified through
some other means internal (or even external) to the program. It is pretty much useful only
for qualifying a pointer parameter, to indicate that this function will not change the data that
argument points to, but other functions may. This is perhaps the most common use of


const in C and C++.

Free download pdf