473,698 Members | 2,393 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 2408
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
40402
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
1607
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
12130
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
95832
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
4879
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
12789
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
11546
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
2256
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
8913
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
8676
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
8608
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9161
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...
0
9029
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8897
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
8867
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
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4370
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
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.