In C, a non const object implicitly converts, without warning, to its const counterpart, so you can write a function, and call it as:
void foo( const char *p );
char bar[] = "bar";
foo( bar );
However, if the function is to be called via a pointer, it seems you can not be const correct without getting a warning:
void foo( void (*callback)(char *p) );
void bar( const char *p );
foo( bar );
bar does not need to modify the string pointed to, and so for proper const correctness, should be declared with const, yet passing it as a callback to a function that is willing to give the callback write access, generates a warning that the pointers are not compatible.
Is this an oversight/defect in the C standard?
1 Answer 1
The fact that you can use a wrapper to avoid the warning should show that there isn't a deep technical reason:
void bar(const char *p) { /* ... */ }
void bar_w(char *p) { bar(p); } /* wrapper */
foo(bar_w); /* instead of foo(bar) */
This is based on then well known fact that you can use a pointer-to-T (for any type T) where a pointer-to-const-T is expected (as in your first example).
The warning is due to §6.7.5.3.15 - Function declarators (from ISO/IEC 9899:TC3):
For two function types to be compatible...
[cut]
Moreover, the parameter type lists, if both are present, shall agree in the number of parameters and in use of the ellipsis terminator; corresponding parameters shall have compatible types.
and const char *p
is not compatible with char *p
.
Anyway the compiler issues just a warning (not an error): maybe the programmer is using the wrong function (with a similar signature) and the warning can help to identify the situation.
If everything is ok an explicit cast / a wrapper function can rapidly resolve the "nuisance".
EDIT
it appears that
char *p
is compatible withconst char *p
, just not the other way around
char *p
can be implicitly converted to const char *p
(§6.3.2.3):
For any qualifier q, a pointer to a non-q-qualified type may be converted to a pointer to the q-qualified version of the type; the values stored in the original and converted pointers shall compare equal
(in other words, const
, volatile
and restrict
qualifiers can be added. The original pointer and the result compare equal. See also Implicit conversions).
E.g.
char n;
const char *p = &n; /* &n has type char * */
This doesn't mean that const char *
(a pointer to const-qualified char) is compatible with char *
(pointer to char):
For two pointer types to be compatible, both shall be identically qualified and both shall be pointers to compatible types
(§6.7.5.1)
The pointer are identically qualified (they aren't qualified!) but they aren't pointer to compatible types (const char
is not compatible with a char
).
-
Excellent citation, but why are they not "compatible"? As shown in my first example, it appears that
char *p
is compatible withconst char *p
, just not the other way around.psusi– psusi2016年04月21日 22:56:25 +00:00Commented Apr 21, 2016 at 22:56 -
@psusi Please, take a look at the editmanlio– manlio2016年04月22日 09:13:45 +00:00Commented Apr 22, 2016 at 9:13
-
It appears that const is a qualifier, so the const version is const qualified, but 6.7.5.1 requires that they be both compatible and identically qualified, so that seems to be the problematic rule.psusi– psusi2016年04月22日 20:35:35 +00:00Commented Apr 22, 2016 at 20:35
const
is moved frombar
tofoo
).