P
Paul Emmons
In writing a function similar to strstr(), I'm calling both of the
arguments "const char *". My compiler (gcc) complains
"warning: return discards qualifiers from pointer target type"
unless I have the function return "const char*" as well
(rather than just "char *").
In turn, any pointer receiving this value when calling the function
must also be "const char *" or the compiler will complain,
"warning: assignment discards qualifiers from pointer target type".
This propogation of the attribute gets rather burdensome,
especially in that one might want to use the function on strings
that are not really constant.
How far along is it important to worry about this? When should one
drop the attribute, e.g. with a cast in the return statement: "return
(char *) p," so that the return value can be merely "char * "?
We can see that library functions such as strstr() and strchr()
themselves do not return const char pointers, even though
they always (if not null) point to a character within their const char
arguments. Is this the best point at which to go and do likewise?
arguments "const char *". My compiler (gcc) complains
"warning: return discards qualifiers from pointer target type"
unless I have the function return "const char*" as well
(rather than just "char *").
In turn, any pointer receiving this value when calling the function
must also be "const char *" or the compiler will complain,
"warning: assignment discards qualifiers from pointer target type".
This propogation of the attribute gets rather burdensome,
especially in that one might want to use the function on strings
that are not really constant.
How far along is it important to worry about this? When should one
drop the attribute, e.g. with a cast in the return statement: "return
(char *) p," so that the return value can be merely "char * "?
We can see that library functions such as strstr() and strchr()
themselves do not return const char pointers, even though
they always (if not null) point to a character within their const char
arguments. Is this the best point at which to go and do likewise?