⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 library_5.html

📁 Linux程序员的工作手册
💻 HTML
📖 第 1 页 / 共 3 页
字号:
The <CODE>strcat</CODE> function is similar to <CODE>strcpy</CODE>, except that thecharacters 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>.  Asingle null character is also always appended to <VAR>to</VAR>, so the totalallocated size of <VAR>to</VAR> must be at least <CODE><VAR>size</VAR> + 1</CODE> byteslonger 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> parameteris computed to avoid overflowing the character array <CODE>buffer</CODE>.<P><PRE>#include &#60;string.h&#62;#include &#60;stdio.h&#62;#define SIZE 10static 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>hellohello, 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 fromBSD.  Note that it is not quite equivalent to <CODE>memmove</CODE>, because thearguments 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 fromBSD.  Note that it is not as general as <CODE>memset</CODE>, because the onlyvalue 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 thecontents of strings and arrays.  As well as checking for equality, thesefunctions can also be used as the ordering functions for sortingoperations.  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 functionsreturn a nonzero value if the strings are <EM>not</EM> equivalent ratherthan if they are.  The sign of the value indicates the relative orderingof the first characters in the strings that are not equivalent:  anegative value indicates that the first string is "less" than thesecond, while a positive value indicates that the first string is "greater".<P>If you are using these functions only to check for equality, you mightfind it makes for a cleaner program to hide them behind a macrodefinition, 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 memorybeginning at <VAR>a1</VAR> against the <VAR>size</VAR> bytes of memory beginningat <VAR>a2</VAR>.  The value returned has the same sign as the differencebetween the first differing pair of bytes (interpreted as <CODE>unsignedchar</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 fortesting equality.  It usually isn't meaningful to do byte-wise orderingcomparisons on arrays of things other than bytes.  For example, abyte-wise comparison on the bytes that make up floating-point numbersisn't likely to tell you anything about the relationship between thevalues of the floating-point numbers.<P>You should also be careful about using <CODE>memcmp</CODE> to compare objectsthat can contain "holes", such as the padding inserted into structureobjects to enforce alignment requirements, extra space at the end ofunions, and extra characters at the ends of strings whose length is lessthan their allocated size.  The contents of these "holes" areindeterminate and may cause strange behavior when performing byte-wisecomparisons.  For more predictable results, perform an explicitcomponent-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 differencebetween 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 caseare 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 caseare 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 arethe 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 someother character set--say, EBCDIC--is used instead, then the glyphsare associated with different numeric codes, and the return valuesand 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 fromthe strict numeric ordering of character codes.  For example, in Spanishmost glyphs with diacritical marks such as accents are not considereddistinct letters for the purposes of collation.  On the other hand, thetwo-character sequence <SAMP>`ll'</SAMP> is treated as a single letter that iscollated immediately after <SAMP>`l'</SAMP>.<P>You can use the functions <CODE>strcoll</CODE> and <CODE>strxfrm</CODE> (declared inthe header file <TT>`string.h'</TT>) to compare strings using a collationordering appropriate for the current locale.  The locale used by thesefunctions 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> isthe same as that for <CODE>strcmp</CODE>.<P>Effectively, the way these functions work is by applying a mapping totransform the characters in a string to a byte sequence that representsthe string's position in the collating sequence of the current locale.Comparing two such byte sequences in a simple fashion is equivalent tocomparing the strings with the locale's collating sequence.<P>The function <CODE>strcoll</CODE> performs this translation implicitly, inorder to do one comparison.  By contrast, <CODE>strxfrm</CODE> performs themapping explicitly.  If you are making multiple comparisons using thesame 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 subsequentlycompare 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 thecollating 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; itcomes 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 thecode 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 moreefficiently using <CODE>strxfrm</CODE>.)<P><PRE>/* This is the comparison function used with <CODE>qsort</CODE>. */intcompare_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. */voidsort_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 collationtransformation determined by the locale currently selected forcollation, and stores the transformed string in the array <VAR>to</VAR>.  Upto <VAR>size</VAR> characters (including a terminating null character) arestored.<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 + -