9.16. String handling
Numerous functions exist to handle strings. In C, a string is an array of
characters terminated by a null. In all cases, the functions expect
a pointer to the first character in the string. The header
<string.h> declares these functions.
The functions for this purpose are:
#include <string.h> void *memcpy(void *s1, const void *s2, size_t n); void *memmove (void *s1, const void *s2, size_t n); char *strcpy(char *s1, const char *s2); char *strncpy(char *s1, const char *s2, size_t n); char *strcat(char *s1, const char *s2); char *strncat(char *s1, const char *s2, size_t n);
- This copies
nbytes from the place pointed to by
s2to the place pointed to by
s1. If the objects overlap, the result is undefined. The value of
- Identical to
memcpy, but works even for overlapping objects. It may be marginally slower, though.
- Both of these copy the string pointed to by
s2into the string pointed to by
s1, including the trailing null.
Strncpywill copy at most
ncharacters, and pad with trailing nulls if
s2is shorter than
ncharacters. If the strings overlap, the behaviour is undefined. They return
- Both append the string in
s1, overwriting the null at the end of
s1. A final null is always written. At most
ncharacters are copied from
strncat, which means that for safety the destination string should have room for its original length (not counting the null) plus
n + 1characters. They return
These comparison functions are used to compare arrays of bytes. This
obviously includes the traditional C strings, which are an array of
char (bytes) with a terminating null. All of these functions
work by comparing a byte at a time, and stopping either when two bytes
differ (in which case they return the sign of the difference between the
two bytes), or the arrays are considered to be equal: no differences were
found, and the length of the arrays was equal to the specified amount, or
the null was found at the end of a string comparison.
For all except
strxfrm, the value returned is less than,
equal to or greater than zero depending on whether the first object was
considered to be less than, equal to or greater than the second.
#include <string.h> int memcmp(const void *s1, const void *s2, size_t n); int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t n); size_t strxfrm(char *to, const char *from, int strcoll(const char *s1, const char *s2);
- Compares the first
ncharacters in the objects pointed to by
s2. It is very dodgy to compare structures in this way, because unions or ‘holes’ caused by alignment padding can contain junk.
- Compares the two strings. This is one of the most commonly used of the string-handling functions.
- As for
strcmp, but compares at most
The string in from is converted (by some magic), and placed wherever to points. At most
maxsizecharacters (including the trailing null) are written into the destination. The magic guarantees that two such transformed strings will give the same comparison with each other for the user's current locale when using strcmp, as when
strcollis applied to the original two strings.
In all cases, the length of the resulting string (not counting its terminating null) is returned. If the value is equal to or greater than maxsize, the contents of *to is undefined. If
s1may be a null pointer.
If the two objects overlap, the behaviour is undefined.
- This function compares the two strings according to the collating sequence specified by the current locale.
#include <string.h> void *memchr(const void *s, int c, size_t n); char *strchr(const char *s, int c); size_t strcspn(const char *s1, const char *s2); char *strpbrk(const char *s1, const char *s2); char *strrchr(const char *s, int c); size_t strspn(const char *s1, const char *s2); char *strstr(const char *s1, const char *s2); char *strtok(const char *s1, const char *s2);
- Returns a pointer to the first occurrence in the initial
(unsigned char)c. Returns null if there is no such occurrence.
- Returns a pointer to the first occurrence of
*s, including the null in the search. Returns null if there is no such occurrence.
- Returns the length of the initial part of the string
s1which contains no characters from
s2. The terminating null is not considered to be part of
- Returns a pointer to the first character in
s1which is any of the characters in
s2, or null if there is none.
- Returns a pointer to the last occurrence in
(char)ccounting the null as part of
s1, or null if there is none.
- Returns the length of the initial part of
s1consisting entirely of characters from
- Returns a pointer to the first occurrence in
s1of the string
s2, or null if there is none.
- Breaks the string in
s1into ‘tokens’, each delimited by one of the characters from
s2and returns a pointer to the first token, or null if there is none. Subsequent calls with
(char *)0as the value of
s1return the next token in sequence, with the extra fun that
s2(and hence the delimiters) may differ on each subsequent call. A null pointer is returned if no tokens remain.
#include <string.h> void *memset(void *s, int c, size_t n); char *strerror(int errnum); size_t strlen(const char *s);
- Sets the
nbytes pointed to by
sto the value of
(unsigned char)c. Returns
- Returns the length of the string
snot counting the terminating null. This is a very widely used function.
- Returns a pointer to a string describing the error number
errnum. This string may be changed by subsequent calls to
strerror. Useful for finding out what the values in