Passing char *p to a function foo(const char *p) is common.
Passing const char *p to a function foo(char *p) gets a warning about >discarding const, as one would expect.
Maybe I'm just not thinking clearly at the moment, but I don't see why >passing char *p using foo(&p) to the function foo(const char **pp) causes >this warning:
[...].c: warning: passing 'char **' to parameter of type 'const char **'
discards qualifiers in nested pointer types
[-Wincompatible-pointer-types-discards-qualifiers]
[...].h: note: passing argument to parameter 'pp' here
extern char * foo(const char **pp);
Is this a bug, or what am I missing? Thanks,
-WBE
Passing char *p to a function foo(const char *p) is common.
Passing const char *p to a function foo(char *p) gets a warning about >>discarding const, as one would expect.
Maybe I'm just not thinking clearly at the moment, but I don't see why >>passing char *p using foo(&p) to the function foo(const char **pp) causes >>this warning:
[...].c: warning: passing 'char **' to parameter of type 'const char **'
discards qualifiers in nested pointer types
[-Wincompatible-pointer-types-discards-qualifiers]
[...].h: note: passing argument to parameter 'pp' here
extern char * foo(const char **pp);
Is this a bug, or what am I missing? Thanks,
Is this a port or a newly made application?
Passing char *p to a function foo(const char *p) is common.
Passing const char *p to a function foo(char *p) gets a warning about discarding const, as one would expect.
Maybe I'm just not thinking clearly at the moment, but I don't see why passing char *p using foo(&p) to the function foo(const char **pp) causes this warning:
[...].c: warning: passing 'char **' to parameter of type 'const char **'
discards qualifiers in nested pointer types
[-Wincompatible-pointer-types-discards-qualifiers]
[...].h: note: passing argument to parameter 'pp' here
extern char * foo(const char **pp);
Maybe I'm just not thinking clearly at the moment, but I don't see why
passing char *p using foo(&p) to the function foo(const char **pp) causes
this warning:
[...].c: warning: passing 'char **' to parameter of type 'const char **'
discards qualifiers in nested pointer types
[-Wincompatible-pointer-types-discards-qualifiers]
[...].h: note: passing argument to parameter 'pp' here
extern char * foo(const char **pp);
It's a question of indirection. No indirection and one level of
indirection will always be considered for implicit conversion. The
second level (and any further levels) aren't automatically considered.
See the entry on implicit conversion on cppreference.com
OK, thanks. That would explain WHY.
As to WHAT, I'll contend that a warning message that says "discards qualifiers" in such a case (the same as the compiler warns for (const
char *) passed to a (char *) argument) is misleading.
... I don't see why passing char *p using foo(&p) to the function
foo(const char **pp) causes this warning:
[...].c: warning: passing 'char **' to parameter of type 'const char **'
discards qualifiers in nested pointer types
[-Wincompatible-pointer-types-discards-qualifiers]
[...].h: note: passing argument to parameter 'pp' here
extern char * foo(const char **pp);
Is this a bug, or what am I missing?
This looks like a C FAQ: http://c-faq.com/ansi/constmismatch.html
comp.lang.c FAQ list · Question 11.10
Q: Why can't I pass a char ** to a function which expects a const char **?
(C++ has more complicated rules for assigning const-qualified pointers
In C, if you must assign or pass pointers which have qualifier
mismatches at other than the first level of indirection, you must use explicit casts (e.g. (const char **) in this case), although as always,
the need for such a cast may indicate a deeper problem which the cast
doesn't really fix.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 355 |
Nodes: | 16 (2 / 14) |
Uptime: | 29:21:57 |
Calls: | 7,658 |
Calls today: | 2 |
Files: | 12,815 |
Messages: | 5,700,934 |