473,387 Members | 3,820 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,387 software developers and data experts.

How to test whether strstr() returns a null pointer or not?

How to test whether strstr() returns a null pointer or not? Do I
use something like the following?

if(NULL != strstr(str1,str2))

That does not seem to work.
Nov 14 '05 #1
24 12537
John Smith <js****@company.com> wrote:
# How to test whether strstr() returns a null pointer or not? Do I
# use something like the following?
#
# if(NULL != strstr(str1,str2))

if (strstr(str1,str2))

always works for me.

#
# That does not seem to work.
#
#

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I'm not even supposed to be here today.
Nov 14 '05 #2
On Sat, 04 Jun 2005 02:56:05 GMT, John Smith <js****@company.com>
wrote:
How to test whether strstr() returns a null pointer or not? Do I
use something like the following?

if(NULL != strstr(str1,str2))

That does not seem to work.


Does not work how? Tell us what you expect and what actually
happens. Provide some compilable code that demonstrates this
behavior.
<<Remove the del for email>>
Nov 14 '05 #3
John Smith wrote on 04/06/05 :
How to test whether strstr() returns a null pointer or not? Do I
use something like the following?

if(NULL != strstr(str1,str2))

That does not seem to work.


Give a complete example. I suspect that the strings pointed by str1 and
str2 are not the ones you are expecting (say, a extra trailing '\n' for
example...).

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"There are 10 types of people in the world today;
those that understand binary, and those that dont."

Nov 14 '05 #4
On Sat, 04 Jun 2005 04:12:50 +0000, SM Ryan wrote:
John Smith <js****@company.com> wrote:
# How to test whether strstr() returns a null pointer or not? Do I
# use something like the following?
#
# if(NULL != strstr(str1,str2))
Yes, that's one way of doing it

if (strstr(str1,str2))


The 2 forms are equivalent so if one doesn't work the other won't either.

Lawrence

Nov 14 '05 #5

"Lawrence Kirby" <lk****@netactive.co.uk> wrote
# if(NULL != strstr(str1,str2))

if (strstr(str1,str2))


The 2 forms are equivalent so if one doesn't work the other won't either.

He might have manged to redefine NULL.

..backwards expressions read to Hard
Nov 14 '05 #6
SM Ryan wrote:
John Smith <js****@company.com> wrote:
# How to test whether strstr() returns a null pointer or not? Do I
# use something like the following?
#
# if(NULL != strstr(str1,str2))

if (strstr(str1,str2))

always works for me.


Thanks. That works.
Nov 14 '05 #7
On Sat, 04 Jun 2005 14:09:19 GMT, John Smith <js****@company.com>
wrote:
SM Ryan wrote:
John Smith <js****@company.com> wrote:
# How to test whether strstr() returns a null pointer or not? Do I
# use something like the following?
#
# if(NULL != strstr(str1,str2))

if (strstr(str1,str2))

always works for me.


Thanks. That works.


That's really odd.

if (NULL != strstr(str1, str2))

should be equivalent to

if (strstr(str1, str2))

which should also be equivalent to

if (0 != strstr(str1, str2))

(the use of NULL instead of 0 is a stylistical thing)

What exactly didn't work in your program? I believe the Standard only
requires the macro NULL to be defined in <stddef.h>. Perhaps if you
include <stddef.h> in your program it will work.

Nov 14 '05 #8
On Sat, 04 Jun 2005 16:35:39 +0200, Paul Mesken <us*****@euronet.nl>
wrote:
I believe the Standard only
requires the macro NULL to be defined in <stddef.h>.


And <locale.h>, <stdlib.h>, <stdio.h>, <string.h>, <time.h>

Furrfu, that Standard could use a better index :-)

Well, in that case NULL might have been redefined as someone else
suggested, since NULL is defined in <string.h> and you use strstr()
(which, I hope, implies that you also included <string.h>
Nov 14 '05 #9
Barry Schwarz wrote:
On Sat, 04 Jun 2005 02:56:05 GMT, John Smith <js****@company.com>
wrote:

How to test whether strstr() returns a null pointer or not? Do I
use something like the following?

if(NULL != strstr(str1,str2))

That does not seem to work.

Does not work how? Tell us what you expect and what actually
happens. Provide some compilable code that demonstrates this
behavior.


Not sure. But if(strstr(str1,str2)) works. So I'll just use that.
Nov 14 '05 #10
On Sat, 04 Jun 2005 14:10:24 GMT, John Smith <js****@company.com>
wrote:
Barry Schwarz wrote:
On Sat, 04 Jun 2005 02:56:05 GMT, John Smith <js****@company.com>
wrote:

How to test whether strstr() returns a null pointer or not? Do I
use something like the following?

if(NULL != strstr(str1,str2))

That does not seem to work.

Does not work how? Tell us what you expect and what actually
happens. Provide some compilable code that demonstrates this
behavior.


Not sure. But if(strstr(str1,str2)) works. So I'll just use that.


Then how do you know it didn't work? The two statements are
equivalent. Your change is cosmetic. What is the real problem? Did
you include string.h? Do you have your warning level set to max?
<<Remove the del for email>>
Nov 14 '05 #11
Barry Schwarz wrote:

Then how do you know it didn't work? The two statements are
equivalent. Your change is cosmetic. What is the real problem? Did
you include string.h? Do you have your warning level set to max?


A window popped up asking for permission to report the problem to
Microsoft. That's how I knew something was wrong. Anyway, since
the program is working now, I'll just leave the mistery behind and
forget about it.
Nov 14 '05 #12
Paul Mesken <us*****@euronet.nl> writes:
On Sat, 04 Jun 2005 14:09:19 GMT, John Smith <js****@company.com>
wrote:
SM Ryan wrote:
John Smith <js****@company.com> wrote:
> How to test whether strstr() returns a null pointer or not? Do I
> use something like the following?
>
> if(NULL != strstr(str1,str2))

if (strstr(str1,str2))

always works for me.

Thanks. That works.


That's really odd.

[...] What exactly didn't work in your program? I believe the Standard only
requires the macro NULL to be defined in <stddef.h>. Perhaps if you
include <stddef.h> in your program it will work.


If the OP didn't #include one of the headers that defines NULL, he
should have gotten a compilation error. Since all he told us was that
"That does not seem to work", I'm assuming it compiled but didn't
behave the way he expected. (Note to everyone posting questions: you
need to tell us *how* it doesn't work.)

If NULL has been redefined somewhere, he needs to track it down and
fix it. Redefining NULL is a very bad idea.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #13
John Smith <js****@company.com> writes:
Barry Schwarz wrote:
Then how do you know it didn't work? The two statements are
equivalent. Your change is cosmetic. What is the real problem? Did
you include string.h? Do you have your warning level set to max?


A window popped up asking for permission to report the problem to
Microsoft. That's how I knew something was wrong. Anyway, since
the program is working now, I'll just leave the mistery behind and
forget about it.


It's very likely that the error was a symptom of a deeper problem
that's going to bite you again later on. The change you made should
not have made any difference. It's up to you whether you want to
spend time tracking it down, but I'd advise doing so.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #14

"Keith Thompson" <ks***@mib.org> wrote
A window popped up asking for permission to report the problem to
Microsoft. That's how I knew something was wrong. Anyway, since
the program is working now, I'll just leave the mistery behind and
forget about it.


It's very likely that the error was a symptom of a deeper problem
that's going to bite you again later on. The change you made should
not have made any difference. It's up to you whether you want to
spend time tracking it down, but I'd advise doing so.

The bug is in the compiler. That's why MS want a bug report.

The thing to do (easier said than done) is to run the code through another
compiler to see if everything is correct.
If you are stuck with a bad compiler, there's nothing much you can do,
except look at unusual constructs to see what is causing it to misbehave, or
what the OP is doing, which is to play with the code and hope the problem
goes away.
Nov 14 '05 #15
"Malcolm" <re*******@btinternet.com> writes:
"Keith Thompson" <ks***@mib.org> wrote
A window popped up asking for permission to report the problem to
Microsoft. That's how I knew something was wrong. Anyway, since
the program is working now, I'll just leave the mistery behind and
forget about it.
It's very likely that the error was a symptom of a deeper problem
that's going to bite you again later on. The change you made should
not have made any difference. It's up to you whether you want to
spend time tracking it down, but I'd advise doing so.

The bug is in the compiler. That's why MS want a bug report.


Is it? Does the "report the problem to Microsoft" message reliably
indicate a bug in Microsoft's compiler, or could it be triggered by a
bug in the program itself? (I have no idea what the answer to that
question is.)
The thing to do (easier said than done) is to run the code through another
compiler to see if everything is correct.
If you are stuck with a bad compiler, there's nothing much you can do,
except look at unusual constructs to see what is causing it to misbehave, or
what the OP is doing, which is to play with the code and hope the problem
goes away.


If it's the result of undefined behavior in the program, recompiling
with another compiler coud very well *seem* to correct the problem.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #16
"Keith Thompson" <ks***@mib.org> wrote
A window popped up asking for permission to report the problem to
Microsoft. That's how I knew something was wrong. Anyway, since
the program is working now, I'll just leave the mistery behind and
forget about it.

It's very likely that the error was a symptom of a deeper problem
that's going to bite you again later on. The change you made should
not have made any difference. It's up to you whether you want to
spend time tracking it down, but I'd advise doing so.
The bug is in the compiler. That's why MS want a bug report.


Is it? Does the "report the problem to Microsoft" message reliably
indicate a bug in Microsoft's compiler, or could it be triggered by a
bug in the program itself? (I have no idea what the answer to that
question is.)

A bit OT, but yes.
Microsoft aren't interested if a developer has introduced a bug into his
program, except that obviously the compiler produces a diagnostic if
possible.
However they are interested if their own compiler / ide / debugger isn't
working. The message indicates that their program has encountered an
internal error, i.e. is bugged. (If it wasn't bugged before it popped up the
message box, it is bugged after).
That bug could be triggered by attempting to compile a legal program, or it
could be triggered by an illegal program. Either way it is Microsoft's
fault.
If it's the result of undefined behavior in the program, recompiling
with another compiler coud very well *seem* to correct the problem.

What I should have asked if whether the message appears at compile time or
run time. If it appears when the program is run under the debugger it is
probably a bug in the debugger rather than the compiler itself. It could
indeed be because the debugger cannot cope with undefined behaviour.
You are right that compiling on another compiler may mask the error. Once
your tool stop working reliably it is hard to know what to do.
Nov 14 '05 #17
# if (NULL != strstr(str1, str2))
#
# should be equivalent to
#
# if (strstr(str1, str2))

You're presuming what NULL and strstr are defined to be.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Who's leading this mob?
Nov 14 '05 #18
Malcolm wrote:
"Keith Thompson" <ks***@mib.org> wrote


talking about "if(NULL != strstr(str1,str2))" failing.
A window popped up asking for permission to report the problem to
Microsoft. That's how I knew something was wrong. Anyway, since
the program is working now, I'll just leave the mistery behind and
forget about it.


It's very likely that the error was a symptom of a deeper problem
that's going to bite you again later on. The change you made
should not have made any difference. It's up to you whether you
want to spend time tracking it down, but I'd advise doing so.

The bug is in the compiler. That's why MS want a bug report.

The thing to do (easier said than done) is to run the code through
another compiler to see if everything is correct. If you are stuck
with a bad compiler, there's nothing much you can do, except look
at unusual constructs to see what is causing it to misbehave, or
what the OP is doing, which is to play with the code and hope the
problem goes away.


Please don't snip attributions for material you quote.

Assuming the author hasn't done some thing exorbitantly stupid,
such as redefining NULL, it seems we have the usual Microsloth
quality. If they can't correctly parse such a simple statement,
there must be no regression suite of tests in existance.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #19
CBFalconer wrote:

Malcolm wrote:
"Keith Thompson" <ks***@mib.org> wrote
talking about "if(NULL != strstr(str1,str2))" failing.

Assuming the author hasn't done some thing exorbitantly stupid,


Has it been established yet that <string.h> was #included?

--
pete
Nov 14 '05 #20
"Malcolm" <re*******@btinternet.com> wrote:
"Keith Thompson" <ks***@mib.org> wrote
A window popped up asking for permission to report the problem to
Microsoft. That's how I knew something was wrong. Anyway, since
the program is working now, I'll just leave the mistery behind and
forget about it.


It's very likely that the error was a symptom of a deeper problem
that's going to bite you again later on. The change you made should
not have made any difference. It's up to you whether you want to
spend time tracking it down, but I'd advise doing so.

The bug is in the compiler. That's why MS want a bug report.


You've never used WinXP, have you?

Richard
Nov 14 '05 #21
Keith Thompson wrote:
"Malcolm" <re*******@btinternet.com> writes:
"Keith Thompson" <ks***@mib.org> wrote
A window popped up asking for permission to report the problem to
Microsoft. That's how I knew something was wrong. Anyway, since
the program is working now, I'll just leave the mistery behind and
forget about it.

It's very likely that the error was a symptom of a deeper problem
that's going to bite you again later on. The change you made should
not have made any difference. It's up to you whether you want to
spend time tracking it down, but I'd advise doing so.


The bug is in the compiler. That's why MS want a bug report.


Is it? Does the "report the problem to Microsoft" message reliably
indicate a bug in Microsoft's compiler, or could it be triggered by a
bug in the program itself? (I have no idea what the answer to that
question is.)


<OT>
For the record, and from personal experience, I can state categorically
that it can be triggered by a bug in a program. I know this because I
have had it from code written using Delphi and found that the problem
really was a problem in the code I had compiled.
</OT>
The thing to do (easier said than done) is to run the code through another
compiler to see if everything is correct.
If you are stuck with a bad compiler, there's nothing much you can do,
except look at unusual constructs to see what is causing it to misbehave, or
what the OP is doing, which is to play with the code and hope the problem
goes away.


If it's the result of undefined behavior in the program, recompiling
with another compiler coud very well *seem* to correct the problem.


In fact, I would use this as an indication that the program almost
certainly invokes undefined behaviour rather than thinking it means a
compiler bug. I *have* found compiler bugs, but not anywhere near as
often as I've found bugs in the code being compiled.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #22
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:46************@brenda.flash-gordon.me.uk...
Keith Thompson wrote:
"Malcolm" <re*******@btinternet.com> writes:
"Keith Thompson" <ks***@mib.org> wrote

>A window popped up asking for permission to report the problem to
>Microsoft. That's how I knew something was wrong. Anyway, since
>the program is working now, I'll just leave the mistery behind and
> forget about it.

It's very likely that the error was a symptom of a deeper problem
that's going to bite you again later on. The change you made should
not have made any difference. It's up to you whether you want to
spend time tracking it down, but I'd advise doing so.

The bug is in the compiler. That's why MS want a bug report.


Is it? Does the "report the problem to Microsoft" message reliably
indicate a bug in Microsoft's compiler, or could it be triggered by a
bug in the program itself? (I have no idea what the answer to that
question is.)


<OT>
For the record, and from personal experience, I can state categorically
that it can be triggered by a bug in a program. I know this because I have
had it from code written using Delphi and found that the problem really
was a problem in the code I had compiled.
</OT>


When did this become an 'html' group?

#ifdef off_topic_permitted

The 'report the problem to Microsoft' message may pop up whenever
an application crashes (dependant on how you set up windows)

In XP - system properties -> advanced tab -> error reporting button.

Has absolutely NOTHING to do with the compiler... unless it's the
compiler that crashed which caused the message!

#endif
Nov 14 '05 #23
"Mark" <so***@localbar.com> writes:
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message [...]
<OT>
For the record, and from personal experience, I can state categorically
that it can be triggered by a bug in a program. I know this because I have
had it from code written using Delphi and found that the problem really
was a problem in the code I had compiled.
</OT>


When did this become an 'html' group?


It isn't; there is no "OT" tag in HTML. (Yeah, I'm taking the joke
too seriously.)
#ifdef off_topic_permitted

The 'report the problem to Microsoft' message may pop up whenever
an application crashes (dependant on how you set up windows)

In XP - system properties -> advanced tab -> error reporting button.

Has absolutely NOTHING to do with the compiler... unless it's the
compiler that crashed which caused the message!

#endif


Or unless it was a bug in the compiler that caused the application to
crash.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #24
Keith Thompson <ks***@mib.org> wrote:
It isn't; there is no "OT" tag in HTML. (Yeah, I'm taking the joke
too seriously.)
It is XML, however...
Or unless it was a bug in the compiler that caused the application to
crash.


FWIW, in my experience this is not as unlikely as one might suppose.
We use a two-version old Borland suite, and we've learned that certain
operations are likely to trigger fatal compiler bugs...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #25

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

Similar topics

11
by: Squid Seven | last post by:
I create a pointer to an item: CardSession *cardSession; Then, later, I use new to create an instance of the item and assign it to that pointer: cardSession = new CardSession(); In...
34
by: Andrew | last post by:
Is there anyway to test if a pointer points to allocated memory or not? For example if I have a pointer such as char *p is there a standard way to test whether an assignment such as the following...
12
by: Lars Langer | last post by:
Hi there, I'm new to this C - never the less - I'm trying to search a string for the occurence of a substring - However, I'm not very succesful since my use of the strstr function always returns...
5
by: OzBob | last post by:
Am performing the following check to determine if a set of text is at the start of a string,.... /* Check for literal text DD at beginning of string date_format */ char date_format; if...
6
by: linq936 | last post by:
Hi, I have the following code: #include <stdio.h> int main(void){ char* str1 = "abc"; char* str2 = '\0'; if ( strstr(str1, str2) == NULL ){ printf("yes\n");
3
by: lovecreatesbea... | last post by:
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.