strncmp(a,b,0)

J

jacob navia

What is the result of that?

I am reimplementing the C library, and I would like to know.

Now I have it to return zero. Is that correct?

The standard says:The strncmp function compares not more than n characters (characters
that follow a
null character are not compared) from the array pointed to by s1 to the
array pointed to
by s2.
Returns
The strncmp function returns an integer greater than, equal to, or less
than zero,
accordingly as the possibly null-terminated array pointed to by s1 is
greater than, equal
to, or less than the possibly null-terminated array pointed to by s2.
(7.21.4.4 page 328)The zero case is not mentioned.

The comparison of no characters is always equal... (What a nonsense
isn't it?). But I *have* to return something.
 
K

kevin.bagust

What is the result of that?
I am reimplementing the C library, and I would like to know.
Now I have it to return zero. Is that correct?
The standard says: snip
The zero case is not mentioned.
The comparison of no characters is always equal... (What a nonsense
isn't it?). But I *have* to return something.

It is mentions it in the text describing the header file 7.21.1#2.

Where an argument declared as size_t n specifies the length of the arrray
for a function, n can have the value zero on a call to that function.
......
On such a call, ...., a function that compares two character sequences
returns zero, ....

Hope that helps.

Kevin.
 
E

Eric Sosman

jacob said:
What is the result of that?

I am reimplementing the C library, and I would like to know.

Now I have it to return zero. Is that correct?

The standard says:
The strncmp function compares not more than n characters (characters
that follow a
null character are not compared) from the array pointed to by s1 to the
array pointed to
by s2.
Returns
The strncmp function returns an integer greater than, equal to, or less
than zero,
accordingly as the possibly null-terminated array pointed to by s1 is
greater than, equal
to, or less than the possibly null-terminated array pointed to by s2.
(7.21.4.4 page 328)
The zero case is not mentioned.

The comparison of no characters is always equal... (What a nonsense
isn't it?). But I *have* to return something.

Return a zero. Section 7.21.1, paragraph 2 describes
what happens with zero lengths:

[...] On such a call [...] a function that compares
two character sequences returns zero, [...]
 
C

CBFalconer

jacob said:
What is the result of that?

of what?
I am reimplementing the C library, and I would like to know.
Now I have it to return zero. Is that correct?
The standard says:

Snipped and requoted in a legible manner:

7.21.4.4 The strncmp function

Synopsis

[#1]
#include <string.h>
int strncmp(const char *s1, const char *s2, size_t n);

Description

[#2] The strncmp function compares not more than n
characters (characters that follow a null character are not
compared) from the array pointed to by s1 to the array
pointed to by s2.

Returns

[#3] The strncmp function returns an integer greater than,
equal to, or less than zero, accordingly as the possibly
null-terminated array pointed to by s1 is greater than,
equal to, or less than the possibly null-terminated array
pointed to by s2.
The zero case is not mentioned.

The comparison of no characters is always equal... (What a
nonsense isn't it?). But I *have* to return something.

Don't you think one empty string is equal to another empty string?
How about (untested):

while (*s1 && (*s1 == *s2) && n) {
s1++; s2++; n--;
}
return ((*s1 > *s2) - (*s1 < *s2)) && n;

The final comparison may need casts to unsigned. As the standard
reads it would seem that signed chars would be allowed to affect
the result, but there may be a clause specifying this somewhere.
This should be fodder for gccs optimizer or a good assembly
programmer.
 
P

pete

CBFalconer said:
jacob said:
What is the result of that?

of what?
I am reimplementing the C library, and I would like to know.
Now I have it to return zero. Is that correct?
The standard says:

Snipped and requoted in a legible manner:

7.21.4.4 The strncmp function

Synopsis

[#1]
#include <string.h>
int strncmp(const char *s1, const char *s2, size_t n);

Description

[#2] The strncmp function compares not more than n
characters (characters that follow a null character are not
compared) from the array pointed to by s1 to the array
pointed to by s2.

Returns

[#3] The strncmp function returns an integer greater than,
equal to, or less than zero, accordingly as the possibly
null-terminated array pointed to by s1 is greater than,
equal to, or less than the possibly null-terminated array
pointed to by s2.
The zero case is not mentioned.

The comparison of no characters is always equal... (What a
nonsense isn't it?). But I *have* to return something.

Don't you think one empty string is equal to another empty string?
How about (untested):

while (*s1 && (*s1 == *s2) && n) {
s1++; s2++; n--;
}
return ((*s1 > *s2) - (*s1 < *s2)) && n;

return ((*s1 > *s2) - (*s1 < *s2)) && n;

The final comparison may need casts to unsigned. As the standard
reads it would seem that signed chars would be allowed to affect
the result, but there may be a clause specifying this somewhere.
This should be fodder for gccs optimizer or a good assembly
programmer.

In the final comparison, the pointers need to be cast
to type pointer to unsigned char.
If char is signed and *s1 equals -1 and *s2 equals -2,
then which one compares greater,
depends on the representation of negative integers.
 
P

pete

pete said:
CBFalconer wrote:

strncpy is not supposed to compare more than n characters.
I don't see what difference it could make
if strncpy was implemented as above instead.

Apparently I added the following line accidentally.
 
D

Dan Pop

In said:
OK. Thanks, I did not see that one

Because you don't know how to read the C standard. Individual
function descriptions MUST be read in the context set by the introductory
paragraph(s) of each section.

Dan
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top