Programming in C

(Barry) #1
472 Appendix B The Standard C Library

is found and returns a pointer to the start of the token. On subsequent calls,s1
should be a null pointer.When no more tokens remain, a null pointer is returned.
size_t strxfrm (s1, s2, n)
Transforms up to ncharacters from the string s2, placing the result in s1.Two such
transformed strings from the current locale can then be compared with strcmp.

Memory Functions


The following routines deal with arrays of characters.They are designed for efficient
searching of memory and for copying data from one area of memory to another.They
require the header file <string.h>:
#include <string.h>
In the description of these routines,m1and m2are of type void *,cis an intthat gets
converted by the routine to an unsigned char, and nis an integer of type size_t.
void *memchr (m1, c, n)
Searches m1for the first occurrence of c,returning a pointer to it if found, or the null
pointer, if not found after examining ncharacters.
void *memcmp (m1, m2, n)
Compares the corresponding first ncharacters from m1and m2. Zero is returned if
both arrays are identical in their first ncharacters. If they’re not, the difference
between the corresponding characters from m1and m2that caused the first mismatch
is returned. So, if the disagreeing character from m1was less than the corresponding
character from m2,a value less than zero is returned; otherwise, a value greater than
zero is returned.
void *memcpy (m1, m2, n)
Copies ncharacters from m2to m1,returning m1.
void *memmove (m1, m2, n)
Is like memcpy,but is guaranteed to work even if m1and m2overlap in memory.
void *memset (m1, c, n)
Sets the first ncharacters of m1to the value c.memsetreturns m1.
Note that these routines attach no special significance to null characters inside the arrays.
They can be used with arrays other than character arrays provided you cast the pointers
accordingly to void *.So,if data1and data2are each an array of 100 ints, the call
memcpy ((void *) data2, (void *) data1, sizeof (data1));
copies all 100 integers from data1to data2.

21 0672326663 AppB 6/10/04 2:03 PM Page 472

Free download pdf