Suppress 'qualifiers discarding' warning for strstr()

L

lovecreatesbea...

I'm getting `warning: return discards qualifiers from pointer target
type' at line 11 on this code from gcc.

Some people suggested in old posts that this kind of warning can be
suppressed by qualifying the return type with const. In this code it
also can be removed by removing const from the first pointer parameter
str. Are there better ways to do it, or does it deserve to do this?


/* Search str for sub and return the pointer to the beginning of the
first occurence, or return null for no match. */

char *strstr(const char *str, const char *sub)
{
int i, j;

for (i = 0; str; i++)
{
for (j = 0; sub[j]; j++)
if (str[i + j] != sub[j])
break;
if (!sub[j])
return &str; /*line 11*/
}
return 0;
}

Thank you for your time.
 
I

Ian Collins

I'm getting `warning: return discards qualifiers from pointer target
type' at line 11 on this code from gcc.
Fix the code, th waning is a good one.
Some people suggested in old posts that this kind of warning can be
suppressed by qualifying the return type with const. In this code it
also can be removed by removing const from the first pointer parameter
str. Are there better ways to do it, or does it deserve to do this?
The return type should be const, it is a pointer to a cont object.
/* Search str for sub and return the pointer to the beginning of the
first occurence, or return null for no match. */

char *strstr(const char *str, const char *sub)
{
int i, j;

for (i = 0; str; i++)
{
for (j = 0; sub[j]; j++)
if (str[i + j] != sub[j])
break;
if (!sub[j])
return &str; /*line 11*/


str is const char*, so is &str.
 
E

Eric Sosman

Ian Collins wrote On 11/20/07 13:33,:
Fix the code, th waning is a good one.



The return type should be const, it is a pointer to a cont object.

If the code is supposed to work the same way as the
Standard library's strstr(), that's not a solution.
/* Search str for sub and return the pointer to the beginning of the
first occurence, or return null for no match. */

char *strstr(const char *str, const char *sub)
{
int i, j;

for (i = 0; str; i++)
{
for (j = 0; sub[j]; j++)
if (str[i + j] != sub[j])
break;
if (!sub[j])
return &str; /*line 11*/



str is const char*, so is &str.


... and the answer is to convert it from `const char*'
to plain `char*' with a cast:

return (char*)&str;
or
return (char*)str + i;

.... or other equivalents. This is one of those places where
C's type-matching rules are a hindrance rather than a help,
and you have to be rude to them to get them to shut up and
leave you alone.
 
M

Malcolm McLean

I'm getting `warning: return discards qualifiers from pointer target
type' at line 11 on this code from gcc.

/* Search str for sub and return the pointer to the beginning of the
first occurence, or return null for no match. */

char *strstr(const char *str, const char *sub)
The problem is that const has been tacked onto the language instead of being
incorporated from the initial design.
Some standard library functions, including strstr(), simply cannot be
written sensibly with the const paradigm. The problem is that strstr has got
to know whether the parameters are "really const" or not, and then return
either a const pointer or a plain pointer. There is no way of expressing
that, so all you cna do is return a plain pointer and cast the constness
away, thus losing the protection.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top