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) (e-mail address removed) <
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"