[comp.lang.c] result of strstr

todd@cfa.harvard.edu (Todd Karakashian) (05/20/91)

Is the result of strstr(cs,ct) defined when ct is a null string 
(i.e., when *ct == '\0')?  

SunOS 4.1.1 strstr() returns cs in this situation.  I checked the man
page, K&R 2, and the ANSI standard but couldn't find any illuminating
information (like, for example, saying it was undefined).

I am thinking perhaps that strstr() ought to return NULL in this case, or
maybe a pointer to the null termination of cs, but I am curious what
other people think.  

Thanks...

				Todd


########################################################################
## Todd Karakashian		          todd@cfa.harvard.edu (I-net)##
## Smithsonian Astrophysical Observatory  ...harvard!cfa!todd (uucp)  ##
## 60 Garden Street, MS 70		  todd@cfa (BITNET)           ##
## Cambridge, MA  02138 USA	          Phone: 617-495-7168         ##
########################################################################

-- 
########################################################################
## Todd Karakashian		          todd@cfa.harvard.edu (I-net)##
## Smithsonian Astrophysical Observatory  ...harvard!cfa!todd (uucp)  ##
## 60 Garden Street, MS 70		  todd@cfa (BITNET)           ##

todd@cfa.harvard.edu (Todd Karakashian) (05/21/91)

From article <1991May20.163227.7166@cfa.harvard.edu>, by todd@cfa.harvard.edu (Todd Karakashian):
> 
> Is the result of strstr(cs,ct) defined when ct is a null string 
> (i.e., when *ct == '\0')?  
> 
> SunOS 4.1.1 strstr() returns cs in this situation.  I checked the man
> page, K&R 2, and the ANSI standard but couldn't find any illuminating
> information (like, for example, saying it was undefined).
> 
> I am thinking perhaps that strstr() ought to return NULL in this case, or
> maybe a pointer to the null termination of cs, but I am curious what
> other people think.  
> 
> Thanks...
> 
> 				Todd
> 

Well!  Having now perused the ANSI standard more carefully, I find in
4.11.5.7:

	"if s2 points to a string of zero length, the function
returns s1."

Whoops - very sorry to bother you.  In the words of Emily Latella:

			"Never mind."


-- 
########################################################################
## Todd Karakashian		          todd@cfa.harvard.edu (I-net)##
## Smithsonian Astrophysical Observatory  ...harvard!cfa!todd (uucp)  ##
## 60 Garden Street, MS 70		  todd@cfa (BITNET)           ##

enag@ifi.uio.no (Erik Naggum) (05/21/91)

Todd Karakashian writes:
|
|   Is the result of strstr(cs,ct) defined when ct is a null string
|   (i.e., when *ct == '\0')?
|
|   SunOS 4.1.1 strstr() returns cs in this situation.  I checked the man
|   page, K&R 2, and the ANSI standard but couldn't find any illuminating
|   information (like, for example, saying it was undefined).
|
|   I am thinking perhaps that strstr() ought to return NULL in this case, or
|   maybe a pointer to the null termination of cs, but I am curious what
|   other people think.

The null string matches any string, including the null string.

4.11.5.7 in dpANS C of 1/11/88 (which is the one I have handy -- time
to drag the ANSI standard with me in addition to so many other bulky
standards, it seems) clearly states:

	#include <string.h>
	char *strstr (const char *s1, const char *s2);

	...

	Returns

	    The strstr function returns a pointer to the located
	string, or a null pointer if the string is not found.  If s2
	points to a string with zero length, the function returns s1.

</Erik>
--
Erik Naggum             Professional Programmer            +47-2-836-863
Naggum Software             Electronic Text            <enag@ifi.uio.no>
0118 OSLO, NORWAY       Computer Communications      <erik@naggum.uu.no>

gwyn@smoke.brl.mil (Doug Gwyn) (05/21/91)

In article <1991May20.163227.7166@cfa.harvard.edu> todd@cfa.harvard.edu (Todd Karakashian) writes:
>Is the result of strstr(cs,ct) defined when ct is a null string 
>(i.e., when *ct == '\0')?  
>SunOS 4.1.1 strstr() returns cs in this situation.  I checked the man
>page, K&R 2, and the ANSI standard but couldn't find any illuminating
>information (like, for example, saying it was undefined).

Of the handful of lines of text in the C standard describing strstr(),
almost an entire line is dedicated to answering exactly that question.
I would argue that it is also redundant, in that the main description
applies equally to the case of a zero-length string, but for some
reason people seem to get confused by zero-sized things.