String handling (string.h)¶
Include string.h for byte-string and raw memory operations.
Functions¶
| Function | Summary |
|---|---|
void * memchr( const void *, int, size_t ) |
Find the first matching byte within n bytes of an object. |
int memcmp( const void *, const void *, size_t ) |
Compare n bytes from two objects. |
void * memcpy( void *, const void *, size_t ) |
Copy n bytes from a non-overlapping source object to a destination object. |
void * memmove( void *, const void *, size_t ) |
Copy n bytes between objects, safely handling overlap. |
void * memset( void *, int, size_t ) |
Fill n bytes of an object with the low byte of c. |
char * strcat( char *, const char * ) |
Append one string to another. |
char * strchr( const char *, int ) |
Find the first occurrence of a character in a string. |
int strcmp( const char *, const char * ) |
Lexicographic string comparison. |
int strcoll( const char *, const char * ) |
Locale-independent string comparison, equivalent to strcmp in dcc. |
char * strcpy( char *, const char * ) |
Copy a string including its terminating NUL. |
size_t strcspn( const char *, const char * ) |
Length of the leading segment containing none of the reject characters. |
char * strdup( const char * ) |
Allocate and return a heap copy of a string. |
char * strerror( int ) |
Return text for an error number. |
int stricmp( const char *, const char * ) |
Case-insensitive lexicographic string comparison (ASCII only). |
size_t strlen( const char * ) |
Length of a string excluding the terminating NUL. |
char * strncat( char *, const char *, size_t ) |
Append at most n characters from one string to another, then write a NUL. |
int strncmp( const char *, const char *, size_t ) |
Compare at most n characters from two strings. |
char * strncpy( char *, const char *, size_t ) |
Copy at most n characters from one string to another, padding with NULs. |
char * strpbrk( const char *, const char * ) |
Find the first character from a set in a string. |
char * strrchr( const char *, int ) |
Find the last occurrence of a character in a string. |
size_t strspn( const char *, const char * ) |
Length of the leading segment containing only accepted characters. |
char * strstr( const char *, const char * ) |
Find the first occurrence of a substring in a string. |
char * strtok( char *, const char * ) |
Split a string into tokens separated by delimiter characters. |
size_t strxfrm( char *, const char *, size_t ) |
Transform s2 for strcoll comparison into s1, writing at most n chars; returns strlen(s2). |
Runtime model¶
The string and memory functions operate on byte strings and raw byte buffers.
strcoll is equivalent to strcmp; the DCC C Compiler has no locale model.
String and memory operations¶
#include <string.h>
char dst[16];
strcpy(dst, "hello");
if (strcmp(dst, "hello") == 0)
memset(dst, 0, sizeof dst);
Tokenizing a copy of a string with strtok writes NULs into the buffer. Pass
only modifiable strings; do not pass string literals:
#include <stdio.h>
#include <string.h>
char line[] = "alpha,beta,,gamma";
char *tok = strtok(line, ",");
while (tok) {
puts(tok); /* alpha, beta, gamma */
tok = strtok(NULL, ",");
}
strdup is the exception
Most string routines link nothing extra, but strdup allocates, so it
inherits the whole malloc chain. See the
appendix.