Efficient STRing CoMPares?

Lars Wirzenius wirzenius at cc.helsinki.fi
Tue Mar 19 22:32:18 AEST 1991


In article <1991Mar18.174207.7377 at bingvaxu.cc.binghamton.edu>, consp06 at bingsuns.cc.binghamton.edu (Robert Konigsberg) writes:
> Somebody said part of the problem with string comparisons is that if
> the most of the strings are equivelant, there will be alot of
> processor time.  Wouldn't it be good then, to include in the macro,
> something to compare the actual POINTERS?  If the pointers are the
> same then the two strings have no CHOICE but to be equivelant.  This
> would really cut down the time under certain situations.

No, two strings may be equivalent, even if they are stored in different
places (this applies to string constants too, if they appear several
times). For example:

	char *s = "hello";
	char *t = "hello";

s and t may or may not point to the same character, you can't depend on
either behaviour. Another example:

	char s[10] = "hello"; 
	char t[10];

	strcpy(t, s);

Now the contents (up to and including the terminating '\0', but no
longer of course) of s and t are the same, but the addresses of the two
variables are different.

Of course, if two pointers to char are equivalent, the strings will be
the same, but this isn't the only occurence, nor do I think it is all
that common.

-- 
Lars Wirzenius    wirzenius at cc.helsinki.fi



More information about the Comp.lang.c mailing list