473,732 Members | 2,201 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2409
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.comwr ote 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
40406
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 without having to modify the PHP.ini file? Krishna Srinivasan.
4
1609
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 string constant AddFans.cp line 343 char *start1 = strstr(start, " First Name="); //this one is due to the fact that for the life of me i cant get strstr to recognize a '\r'
9
12157
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. (State:01003, Native Code: 0) Is there anyway to suppress this message in my report? Thanks
6
95842
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 pointer target type Does anyone know what this warning means? Why do I get it? The program compiles and appears to work, but I'd like to understand the warning. Here is basically what the code looks like: char *str;
12
4884
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 -Wall -Wunused -Werror -W -Wmissing-prototypes -Wconversion -Wshadow -Wpointer-arith -Wcast-qual -Wcast-align -Wwrite-strings -c -o foo.o foo.c
4
12794
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 for an answer but no luck. I don't want to fix the cause because it's how the program is supposed to work. It's part of an example in a text and the example suppresses a similar warning in Borland. I am sure there must be a way to do this in...
3
11548
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 logging system will create a default logger that *also* emits logs, which I can't seem to get rid of. Is there a way I can suppress the creation of this default logger, or remove it when I 'm setting up my handlers? Thanks. Sample code:
2
2257
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 class and I'm trying to do some matrix arithmetic where the elements of the matrices are Octonions. (My Octonions are essentially 8-component vectors with a multiplication table that I compute at runtime. Like vectors, they are a template class, but...
4
8914
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 code. What's wrong with these lines? I searched the web for it, but didn't find anything useful. Thanks,
0
8946
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.