473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Suppress 'qualifiers discarding' warning for strstr()

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.
Nov 20 '07 #1
3 2381
lovecreatesbea...@gmail.com 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.
Nov 20 '07 #2
Ian Collins wrote On 11/20/07 13:33,:
lovecreatesbea...@gmail.com 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.

--
Er*********@sun.com

Nov 20 '07 #3

"lovecreatesbea...@gmail.com" <lo***************@gmail.comwrote 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

Nov 28 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Krishna Srinivasan | last post by:
I have a form with check boxes. When accessing a check box element that is not checked, I get a notice (Notice: Undefined variable..). Is there a way to hide these notices and warning in PHP code...
4
by: Sean Bartholomew | last post by:
even though my code works and everything i keep getting these annoying messages. can anyone help? Path for the user defined root 'CT_INSTALL' cannot be found '' Warning : illegal multi-line...
9
by: Doug Ly | last post by:
Hi, When I run this query using WinSQL to connect to a DB2 database, it gave me the warning: Error: SQLSTATE 01003: Null values were eliminated from the argument of a column function. ...
6
by: Jason | last post by:
I have a function (Inet_ntop) that returns const char * and if I try to assign that return value to a char * variable, I get the gcc error message: warning: assignment discards qualifiers from...
12
by: Charlie Zender | last post by:
Hi, I am unable to compile a large body of code with extremely pedantic compile time checks activate, so that warnings cause errors. With GCC 3.3.1, I do this with gcc -std=c99 -pedantic...
4
by: J Swift | last post by:
I posted this question earlier but on the wrong usenet. I have a warning below that I need to suppress *warning C4018: '<' : signed/unsigned mismatch I searched google, Microsoft help and MSDN...
3
by: Chris Shenton | last post by:
I am setting up handlers to log DEBUG and above to a rotating file and ERROR and above to console. But if any of my code calls a logger (e.g., logging.error("foo")) before I setup my handlers, the...
2
by: Thelma Lubkin | last post by:
I use my own matrix and vector classes. I wrote them before such things were generally available and I've stuck with them ever since. I've just added an Octonion class derived from the vectors...
4
by: Andre | last post by:
Hi All, When I compile the following piece of code with gcc, I get 3 "warning: initialization discards qualifiers from pointer target type" messages which refer to the 3 lines marked in the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.