?? library_5.html
字號:
The <CODE>strcat</CODE> function is similar to <CODE>strcpy</CODE>, except that the
characters from <VAR>from</VAR> are concatenated or appended to the end of
<VAR>to</VAR>, instead of overwriting it. That is, the first character from
<VAR>from</VAR> overwrites the null character marking the end of <VAR>to</VAR>.
<P>
An equivalent definition for <CODE>strcat</CODE> would be:
<P>
<PRE>
char *
strcat (char *to, const char *from)
{
strcpy (to + strlen (to), from);
return to;
}
</PRE>
<P>
This function has undefined results if the strings overlap.
<P>
<A NAME="IDX293"></A>
<U>Function:</U> char * <B>strncat</B> <I>(char *<VAR>to</VAR>, const char *<VAR>from</VAR>, size_t <VAR>size</VAR>)</I><P>
This function is like <CODE>strcat</CODE> except that not more than <VAR>size</VAR>
characters from <VAR>from</VAR> are appended to the end of <VAR>to</VAR>. A
single null character is also always appended to <VAR>to</VAR>, so the total
allocated size of <VAR>to</VAR> must be at least <CODE><VAR>size</VAR> + 1</CODE> bytes
longer than its initial length.
<P>
<PRE>
char *
strncat (char *to, const char *from, size_t size)
{
strncpy (to + strlen (to), from, size);
return to;
}
</PRE>
<P>
The behavior of <CODE>strncat</CODE> is undefined if the strings overlap.
<P>
Here is an example showing the use of <CODE>strncpy</CODE> and <CODE>strncat</CODE>.
Notice how, in the call to <CODE>strncat</CODE>, the <VAR>size</VAR> parameter
is computed to avoid overflowing the character array <CODE>buffer</CODE>.
<P>
<PRE>
#include <string.h>
#include <stdio.h>
#define SIZE 10
static char buffer[SIZE];
main ()
{
strncpy (buffer, "hello", SIZE);
printf ("%s\n", buffer);
strncat (buffer, ", world", SIZE - strlen (buffer) - 1);
printf ("%s\n", buffer);
}
</PRE>
<P>
The output produced by this program looks like:
<P>
<PRE>
hello
hello, wo
</PRE>
<P>
<A NAME="IDX294"></A>
<U>Function:</U> void * <B>bcopy</B> <I>(void *<VAR>from</VAR>, const void *<VAR>to</VAR>, size_t <VAR>size</VAR>)</I><P>
This is a partially obsolete alternative for <CODE>memmove</CODE>, derived from
BSD. Note that it is not quite equivalent to <CODE>memmove</CODE>, because the
arguments are not in the same order.
<P>
<A NAME="IDX295"></A>
<U>Function:</U> void * <B>bzero</B> <I>(void *<VAR>block</VAR>, size_t <VAR>size</VAR>)</I><P>
This is a partially obsolete alternative for <CODE>memset</CODE>, derived from
BSD. Note that it is not as general as <CODE>memset</CODE>, because the only
value it can store is zero.
<P>
<A NAME="IDX296"></A>
<A NAME="IDX297"></A>
<A NAME="IDX298"></A>
<A NAME="IDX299"></A>
<A NAME="IDX300"></A>
<H2><A NAME="SEC62" HREF="library_toc.html#SEC62" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC62">String/Array Comparison</A></H2>
<P>
You can use the functions in this section to perform comparisons on the
contents of strings and arrays. As well as checking for equality, these
functions can also be used as the ordering functions for sorting
operations. See section <A HREF="library_8.html#SEC86" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_8.html#SEC86">Searching and Sorting</A>, for an example of this.
<P>
Unlike most comparison operations in C, the string comparison functions
return a nonzero value if the strings are <EM>not</EM> equivalent rather
than if they are. The sign of the value indicates the relative ordering
of the first characters in the strings that are not equivalent: a
negative value indicates that the first string is "less" than the
second, while a positive value indicates that the first string is
"greater".
<P>
If you are using these functions only to check for equality, you might
find it makes for a cleaner program to hide them behind a macro
definition, like this:
<P>
<PRE>
#define str_eq(s1,s2) (!strcmp ((s1),(s2)))
</PRE>
<P>
All of these functions are declared in the header file <TT>`string.h'</TT>.
<A NAME="IDX301"></A>
<P>
<A NAME="IDX302"></A>
<U>Function:</U> int <B>memcmp</B> <I>(const void *<VAR>a1</VAR>, const void *<VAR>a2</VAR>, size_t <VAR>size</VAR>)</I><P>
The function <CODE>memcmp</CODE> compares the <VAR>size</VAR> bytes of memory
beginning at <VAR>a1</VAR> against the <VAR>size</VAR> bytes of memory beginning
at <VAR>a2</VAR>. The value returned has the same sign as the difference
between the first differing pair of bytes (interpreted as <CODE>unsigned
char</CODE> objects, then promoted to <CODE>int</CODE>).
<P>
If the contents of the two blocks are equal, <CODE>memcmp</CODE> returns
<CODE>0</CODE>.
<P>
On arbitrary arrays, the <CODE>memcmp</CODE> function is mostly useful for
testing equality. It usually isn't meaningful to do byte-wise ordering
comparisons on arrays of things other than bytes. For example, a
byte-wise comparison on the bytes that make up floating-point numbers
isn't likely to tell you anything about the relationship between the
values of the floating-point numbers.
<P>
You should also be careful about using <CODE>memcmp</CODE> to compare objects
that can contain "holes", such as the padding inserted into structure
objects to enforce alignment requirements, extra space at the end of
unions, and extra characters at the ends of strings whose length is less
than their allocated size. The contents of these "holes" are
indeterminate and may cause strange behavior when performing byte-wise
comparisons. For more predictable results, perform an explicit
component-wise comparison.
<P>
For example, given a structure type definition like:
<P>
<PRE>
struct foo
{
unsigned char tag;
union
{
double f;
long i;
char *p;
} value;
};
</PRE>
<P>
you are better off writing a specialized comparison function to compare
<CODE>struct foo</CODE> objects instead of comparing them with <CODE>memcmp</CODE>.
<P>
<A NAME="IDX303"></A>
<U>Function:</U> int <B>strcmp</B> <I>(const char *<VAR>s1</VAR>, const char *<VAR>s2</VAR>)</I><P>
The <CODE>strcmp</CODE> function compares the string <VAR>s1</VAR> against
<VAR>s2</VAR>, returning a value that has the same sign as the difference
between the first differing pair of characters (interpreted as
<CODE>unsigned char</CODE> objects, then promoted to <CODE>int</CODE>).
<P>
If the two strings are equal, <CODE>strcmp</CODE> returns <CODE>0</CODE>.
<P>
A consequence of the ordering used by <CODE>strcmp</CODE> is that if <VAR>s1</VAR>
is an initial substring of <VAR>s2</VAR>, then <VAR>s1</VAR> is considered to be
"less than" <VAR>s2</VAR>.
<P>
<A NAME="IDX304"></A>
<U>Function:</U> int <B>strcasecmp</B> <I>(const char *<VAR>s1</VAR>, const char *<VAR>s2</VAR>)</I><P>
This function is like <CODE>strcmp</CODE>, except that differences in case
are ignored.
<P>
<CODE>strcasecmp</CODE> is derived from BSD.
<P>
<A NAME="IDX305"></A>
<U>Function:</U> int <B>strncasecmp</B> <I>(const char *<VAR>s1</VAR>, const char *<VAR>s2</VAR>, size_t <VAR>n</VAR>)</I><P>
This function is like <CODE>strncmp</CODE>, except that differences in case
are ignored.
<P>
<CODE>strncasecmp</CODE> is a GNU extension.
<P>
<A NAME="IDX306"></A>
<U>Function:</U> int <B>strncmp</B> <I>(const char *<VAR>s1</VAR>, const char *<VAR>s2</VAR>, size_t <VAR>size</VAR>)</I><P>
This function is the similar to <CODE>strcmp</CODE>, except that no more than
<VAR>size</VAR> characters are compared. In other words, if the two strings are
the same in their first <VAR>size</VAR> characters, the return value is zero.
<P>
Here are some examples showing the use of <CODE>strcmp</CODE> and <CODE>strncmp</CODE>.
These examples assume the use of the ASCII character set. (If some
other character set--say, EBCDIC--is used instead, then the glyphs
are associated with different numeric codes, and the return values
and ordering may differ.)
<P>
<PRE>
strcmp ("hello", "hello")
=> 0 /* These two strings are the same. */
strcmp ("hello", "Hello")
=> 32 /* Comparisons are case-sensitive. */
strcmp ("hello", "world")
=> -15 /* The character <CODE>'h'</CODE> comes before <CODE>'w'</CODE>. */
strcmp ("hello", "hello, world")
=> -44 /* Comparing a null character against a comma. */
strncmp ("hello", "hello, world"", 5)
=> 0 /* The initial 5 characters are the same. */
strncmp ("hello, world", "hello, stupid world!!!", 5)
=> 0 /* The initial 5 characters are the same. */
</PRE>
<P>
<A NAME="IDX307"></A>
<U>Function:</U> int <B>bcmp</B> <I>(const void *<VAR>a1</VAR>, const void *<VAR>a2</VAR>, size_t <VAR>size</VAR>)</I><P>
This is an obsolete alias for <CODE>memcmp</CODE>, derived from BSD.
<P>
<H2><A NAME="SEC63" HREF="library_toc.html#SEC63" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC63">Collation Functions</A></H2>
<A NAME="IDX308"></A>
<A NAME="IDX309"></A>
<P>
In some locales, the conventions for lexicographic ordering differ from
the strict numeric ordering of character codes. For example, in Spanish
most glyphs with diacritical marks such as accents are not considered
distinct letters for the purposes of collation. On the other hand, the
two-character sequence <SAMP>`ll'</SAMP> is treated as a single letter that is
collated immediately after <SAMP>`l'</SAMP>.
<P>
You can use the functions <CODE>strcoll</CODE> and <CODE>strxfrm</CODE> (declared in
the header file <TT>`string.h'</TT>) to compare strings using a collation
ordering appropriate for the current locale. The locale used by these
functions in particular can be specified by setting the locale for the
<CODE>LC_COLLATE</CODE> category; see section <A HREF="library_7.html#SEC76" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_7.html#SEC76">Locales and Internationalization</A>.
<A NAME="IDX310"></A>
<P>
In the standard C locale, the collation sequence for <CODE>strcoll</CODE> is
the same as that for <CODE>strcmp</CODE>.
<P>
Effectively, the way these functions work is by applying a mapping to
transform the characters in a string to a byte sequence that represents
the string's position in the collating sequence of the current locale.
Comparing two such byte sequences in a simple fashion is equivalent to
comparing the strings with the locale's collating sequence.
<P>
The function <CODE>strcoll</CODE> performs this translation implicitly, in
order to do one comparison. By contrast, <CODE>strxfrm</CODE> performs the
mapping explicitly. If you are making multiple comparisons using the
same string or set of strings, it is likely to be more efficient to use
<CODE>strxfrm</CODE> to transform all the strings just once, and subsequently
compare the transformed strings with <CODE>strcmp</CODE>.
<P>
<A NAME="IDX311"></A>
<U>Function:</U> int <B>strcoll</B> <I>(const char *<VAR>s1</VAR>, const char *<VAR>s2</VAR>)</I><P>
The <CODE>strcoll</CODE> function is similar to <CODE>strcmp</CODE> but uses the
collating sequence of the current locale for collation (the
<CODE>LC_COLLATE</CODE> locale).
<P>
Here is an example of sorting an array of strings, using <CODE>strcoll</CODE>
to compare them. The actual sort algorithm is not written here; it
comes from <CODE>qsort</CODE> (see section <A HREF="library_8.html#SEC89" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_8.html#SEC89">Array Sort Function</A>). The job of the
code shown here is to say how to compare the strings while sorting them.
(Later on in this section, we will show a way to do this more
efficiently using <CODE>strxfrm</CODE>.)
<P>
<PRE>
/* This is the comparison function used with <CODE>qsort</CODE>. */
int
compare_elements (char **p1, char **p2)
{
return strcoll (*p1, *p2);
}
/* This is the entry point--the function to sort
strings using the locale's collating sequence. */
void
sort_strings (char **array, int nstrings)
{
/* Sort <CODE>temp_array</CODE> by comparing the strings. */
qsort (array, sizeof (char *),
nstrings, compare_elements);
}
</PRE>
<A NAME="IDX312"></A>
<P>
<A NAME="IDX313"></A>
<U>Function:</U> size_t <B>strxfrm</B> <I>(char *<VAR>to</VAR>, const char *<VAR>from</VAR>, size_t <VAR>size</VAR>)</I><P>
The function <CODE>strxfrm</CODE> transforms <VAR>string</VAR> using the collation
transformation determined by the locale currently selected for
collation, and stores the transformed string in the array <VAR>to</VAR>. Up
to <VAR>size</VAR> characters (including a terminating null character) are
stored.
<P>
The behavior is undefined if the strings <VAR>to</VAR> and <VAR>from</VAR>
overlap; see section <A HREF="library_5.html#SEC61" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_5.html#SEC61">Copying and Concatenation</A>.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -