Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Suppress 'qualifiers discarding' warning for strstr()

Reply
Thread Tools

Suppress 'qualifiers discarding' warning for strstr()

 
 
lovecreatesbea...@gmail.com
Guest
Posts: n/a
 
      11-20-2007
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]; i++)
{
for (j = 0; sub[j]; j++)
if (str[i + j] != sub[j])
break;
if (!sub[j])
return &str[i]; /*line 11*/
}
return 0;
}

Thank you for your time.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      11-20-2007
(E-Mail Removed) wrote:
> 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]; i++)
> {
> for (j = 0; sub[j]; j++)
> if (str[i + j] != sub[j])
> break;
> if (!sub[j])
> return &str[i]; /*line 11*/


str is const char*, so is &str[i].

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-20-2007
Ian Collins wrote On 11/20/07 13:33,:
> (E-Mail Removed) wrote:
>
>>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.


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]; i++)
>> {
>> for (j = 0; sub[j]; j++)
>> if (str[i + j] != sub[j])
>> break;
>> if (!sub[j])
>> return &str[i]; /*line 11*/

>
>
> str is const char*, so is &str[i].


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

return (char*)&str[i];
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      11-28-2007

"(E-Mail Removed)" <(E-Mail Removed)> wrote in message
> 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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: best way to suppress "unused" warning? Antoninus Twink C Programming 6 04-29-2008 09:05 AM
Re: best way to suppress "unused" warning? fred.l.kleinschmidt@boeing.com C Programming 0 04-25-2008 04:12 PM
Howto suppress a warning "insecure world writeable .." in Ruby 1.8.4 ? Nuralanur@aol.com Ruby 2 01-25-2006 02:20 PM
Re: distutils:How suppress warning message? (about nonstandardinstall path) Ian Bicking Python 0 10-21-2003 11:03 PM
distutils:How suppress warning message? (about nonstandard install path) Christian Seberino Python 0 10-21-2003 10:37 PM



Advertisments