Strange strcmp() action?

E

Eric2

Consider this...

char abc[ 10 ] = "qwertyu"; // note abc doesn't get filled

int result = strcmp( &abc[1], "wer" );

"result" should evaluate to zero, right? (... comparing "wer" to the
contents of the string abc starting at position 1...)

It doesn't... it evaluates to 1.

For that matter...

int result = strcmp( abc, "qwe" );

.... also evaluates to 1.

If I say...

char *abc = "qwertyu";

.... strcmp still evaluates to 1 in both examples.

On the other hand...

int result = strncmp( &abc[1], "wer", 3);

.... evaluates to zero just like it should.

I think my computer is haunted... :)
 
J

Josh Holland

Consider this...

char abc[ 10 ] = "qwertyu"; // note abc doesn't get filled

int result = strcmp( &abc[1], "wer" );

"result" should evaluate to zero, right? (... comparing "wer" to the
contents of the string abc starting at position 1...)

It doesn't... it evaluates to 1.

No, your implementation is doing exactly the right thing. If a string is
shorter, it should compare less. For example, see
http://www.gnu.org/software/libc/ma...y-Comparison.html#String_002fArray-Comparison.
"A consequence of the ordering used by strcmp is that if s1 is an
initial substring of s2, then s1 is considered to be “less than†s2."
 
E

Eric Sosman

Consider this...

char abc[ 10 ] = "qwertyu"; // note abc doesn't get filled

Actually, it does: With seven letters and three zeros
('\0', not '0').
int result = strcmp(&abc[1], "wer" );

"result" should evaluate to zero, right? (... comparing "wer" to the
contents of the string abc starting at position 1...)

No: `result' will be non-zero. Positive, in fact, because
"wertyu" is greater than "wer". Or more specifically, because
't' is greater than '\0'.
It doesn't... it evaluates to 1.

All that's guaranteed is the sign (negative, zero, positive),
so any positive result would be valid. 1 is as good as any other.
For that matter...

int result = strcmp( abc, "qwe" );

... also evaluates to 1.

Right: "qwertyu" is greater than "qwe".
If I say...

char *abc = "qwertyu";

... strcmp still evaluates to 1 in both examples.

On the other hand...

int result = strncmp(&abc[1], "wer", 3);

... evaluates to zero just like it should.

Right: 'w'=='w', 'e'=='e', 'r'=='r', and that's all the
characters you told it to compare. All equal, zero result.
I think my computer is haunted... :)

I think you misunderstand what strcmp() does, and how
it differs from strncmp() and memcmp(). The strcmp() function
does not think that "beg" is equal to "beggar" or "begin" or
"begorrah" or "beguine" or even "beg! beg! c'mon, spot, beg!".
Do *you* think all those strings are the same?
 
N

Nick

Eric2 said:
Consider this...

char abc[ 10 ] = "qwertyu"; // note abc doesn't get filled

int result = strcmp( &abc[1], "wer" );

"result" should evaluate to zero, right? (... comparing "wer" to the
contents of the string abc starting at position 1...)

No. strcmp checks for equality. "wertyu" isn't equal to "wer". They
aren't equal, so it doesn't return zero, which it would if they were.
 
R

Richard Bos

Eric2 said:
int result = strcmp( abc, "qwe" );

... also evaluates to 1.
On the other hand...

int result = strncmp( &abc[1], "wer", 3);

... evaluates to zero just like it should.

What I want to know is: if you _know_ that there is a strncmp() as well
as a strcmp() - why on earth do you expect them to do the same thing in
the first place? Surely you realise that, there being two of them, they
were intended to be different?

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,775
Messages
2,569,601
Members
45,182
Latest member
alexanderrm

Latest Threads

Top