473,385 Members | 1,973 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,385 software developers and data experts.

strstr crashes on NULL

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");
}
else{
printf("no\n");
}

return 0;
}

It crashes on strstr() call, so is it because none of strstr()
parameter can be NULL?

Jul 11 '07 #1
6 9331
In article <11**********************@n2g2000hse.googlegroups. com>,
<li*****@hotmail.comwrote:
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");
}
else{
printf("no\n");
}

return 0;
}
It crashes on strstr() call, so is it because none of strstr()
parameter can be NULL?
Right. Your char* str2 = '\0' is setting str2 to a NULL pointer
instead of to an empty string, which would be "" or "\0";
'\0' happens to be an integral constant with value 0 and so is
a valid NULL pointer initializer (if strange). In C89 at least,
strstr() is not defined for the case of a NULL pointer
(but is defined for the case of a pointer to an empty string.)

--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Jul 12 '07 #2
li*****@hotmail.com wrote:
>
Hi,
I have the following code:

#include <stdio.h>
You're missing:
#include <string.h>
>
int main(void){
char* str1 = "abc";
char* str2 = '\0';
That's a bad way to write
char* str2 = NULL;

If you change that to
char* str2 = "";
then you can output "no".
if ( strstr(str1, str2) == NULL ){
printf("yes\n");
}
else{
printf("no\n");
}

return 0;
}

It crashes on strstr() call, so is it because none of strstr()
parameter can be NULL?
Yes.

--
pete
Jul 12 '07 #3
li*****@hotmail.com writes:
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");
}
else{
printf("no\n");
}

return 0;
}

It crashes on strstr() call, so is it because none of strstr()
parameter can be NULL?
Yes. You may also be confused about the various meanings of "null".

In your declaration

char *str2 = '\0';

you're not setting str2 to point to an empty string; you're setting it
to a null pointer, which doesn't point to anything. If you wanted
str2 to point to a empty string, you could have declared:

char *str2 = "";

Normally the character constant '\0' is used to refer to a null
character, the character whose value is zero (sometimes called NUL
with one 'L'). This is the character used to mark the end of a
string.

But as it happens, the character constant '\0' is exactly equivalent
to the integer constant 0, and in this context it indicates a null
pointer, not a null character. Using '\0' in this context isn't
exactly wrong (the compiler is perfectly happy with it), but it's
misleading. If you want a null pointer, it's better to use the NULL
macro.

So, you can either declare:

char *str1 = "abc";
char *str2 = NULL;

(but then passing str2 to strstr invokes undefined behavior), or you
can declare:

char *str1 = "abc";
char *str2 = "";

In that case, strstr(str1, str2) will return the value of str1.

I suggest you take a look at sections 4, 5, and 6 of the comp.lang.c
FAQ, <http://www.c-faq.com/>.

You also need to add a '#include <string.h>' to your program, since
you're calling the strstr function. It may happen to work without it,
but the #include directive is *not* optional. (A note to my fellow
pedants: yes, he could declare strstr himself, but that would be
silly.)

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 12 '07 #4
On Jul 11, 6:33 pm, Keith Thompson <k...@mib.orgwrote:
linq...@hotmail.com writes:
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");
}
else{
printf("no\n");
}
return 0;
}
It crashes on strstr() call, so is it because none of strstr()
parameter can be NULL?

Yes. You may also be confused about the various meanings of "null".

In your declaration

char *str2 = '\0';

you're not setting str2 to point to an empty string; you're setting it
to a null pointer, which doesn't point to anything. If you wanted
str2 to point to a empty string, you could have declared:

char *str2 = "";

Normally the character constant '\0' is used to refer to a null
character, the character whose value is zero (sometimes called NUL
with one 'L'). This is the character used to mark the end of a
string.

But as it happens, the character constant '\0' is exactly equivalent
to the integer constant 0, and in this context it indicates a null
pointer, not a null character. Using '\0' in this context isn't
exactly wrong (the compiler is perfectly happy with it), but it's
misleading. If you want a null pointer, it's better to use the NULL
macro.

So, you can either declare:

char *str1 = "abc";
char *str2 = NULL;

(but then passing str2 to strstr invokes undefined behavior), or you
can declare:

char *str1 = "abc";
char *str2 = "";

In that case, strstr(str1, str2) will return the value of str1.

I suggest you take a look at sections 4, 5, and 6 of the comp.lang.c
FAQ, <http://www.c-faq.com/>.

You also need to add a '#include <string.h>' to your program, since
you're calling the strstr function. It may happen to work without it,
but the #include directive is *not* optional. (A note to my fellow
pedants: yes, he could declare strstr himself, but that would be
silly.)

--
Keith Thompson (The_Other_Keith) k...@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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Thanks.

My intention is that I use '\0' to initialize a string so that later I
can know its state. How about this code:

#include <string.h>
#include <stdio.h>

int main(void){
char str1[10], str2[10];

memset( str1, '\0', 10);
memset( str2, '\0', 10);

strcpy(str1, "abc");
if ( strstr(str1, str2) == NULL ){
printf("yes");
}
else{
printf("no");
}

return 0;
}

This code works fine on the same machine with same compiler, of
course I see "no". I tried commenting out string.h include, it still
works.

So does this mean it is really an un-expected behavior?

Jul 12 '07 #5
li*****@hotmail.com said:

<snip>
My intention is that I use '\0' to initialize a string so that later I
can know its state.
An empty string is very different to a null pointer.

const char *nullpointer = '\0';
const char *emptystring = "\0";

or, equivalently: const char *emptystring = "";
How about this code:

#include <string.h>
#include <stdio.h>

int main(void){
char str1[10], str2[10];

memset( str1, '\0', 10);
memset( str2, '\0', 10);
That's fine, but this works just as well:

char str1[10] = "", str2[10] = "";
strcpy(str1, "abc");
if ( strstr(str1, str2) == NULL ){
Yes, that's fine now.
This code works fine on the same machine with same compiler, of
course I see "no".
Yes - strstr is defined to return str1 if str2 is an empty string.
I tried commenting out string.h include, it still
works.
It is true that the program may give the appearance of continuing to
work when you chop off important bits, in much the same way that a used
car continues to work (for a while) with sand in the carburettor.
So does this mean it is really an un-expected behavior?
The code you posted this time is fine. Perfectly well-defined.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 12 '07 #6
santosh <sa*********@gmail.comwrites:
linq...@hotmail.com wrote:
[...]
> strcpy(str1, "abc");
if ( strstr(str1, str2) == NULL ){
printf("yes");

Unless you terminate the string supplied to printf with a newline
character, '\n', or call fflush(stdout), your output is not guaranteed
to appear immediately
[...]

Actually, it's not guaranteed to appear *at all* unless the last
character you write to stdout is a new-line. It's
implementation-defined whether a text stream must end in a new-line.
The standard doesn't say what happens if a text stream does require a
trailing new-line and you don't provide one, so the behavior is
undefined; you might get no output at all, or demons might fly out of
your nose.

This is another of those things that will *probably* work just the way
you expect it to (the program will print "yes" or "no" without a
new-line), but if you write it correctly in the first place, you don't
have to worry about it.

--
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."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 12 '07 #7

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

Similar topics

0
by: travis ray | last post by:
Hi, I have an extension in which a file object is created in python and passed down to a c extension which attempts to read from it or write to it. Writing to the file pointer seems to work...
5
by: Rob Ristroph | last post by:
Hi, It's pretty unhelpful to post "I have a huge piece of code that crashes in strange places, what's the problem?" but that's basically my problem and I really am at my wit's end. The piece...
3
by: Sean Bartholomew | last post by:
im trying to parse my email file located in my system library on my mac osx. i have a form online that, when filled and submitted, is sent to my email address. i need to parse the fields that...
18
by: Ramprasad A Padmanabhan | last post by:
In my program I am checking wether a emailid exists in a list I have in the complete_list a string like "<aa@abc.com> <ab@abc.com> <bc@abc.com> ... <xy@abc.com>" that is a string of emailids...
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...
24
by: John Smith | last post by:
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.
4
by: smnoff | last post by:
I am trying to use the strstr function but it doesn't seem to work with a dynamically allocated string that I pass into a function. Specifically, I am using a Macromedia C-level extensibility and...
5
by: Jeff | last post by:
ASP.NET 2.0 This code crashes. It generate this error: Value cannot be null. Parameter name: type I've created some custom web.config settings and this crash is related to accessing theme...
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: 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:
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
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
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.