string.h
外观

此條目目前正依照其他维基百科上的内容进行翻译。 (2010年11月25日) |
C标准函数库 |
---|
一般 |
杂项 |
string.h是C语言中C标准库的头文件,其中包含了宏定义、常量以及函数和类型的声明,涉及的内容除了字符串处理之外,还包括大量的内存处理函数;因此,string.h
这个命名是不恰当的。
在string.h
中定义的函数十分常用,作为C标准库的一部分,它们被强制要求可以在任何支持C语言的平台上运行。但是,部分函数存在一些安全隐患,例如缓存溢出等,导致程序员宁愿使用一些更安全的函数而放弃一定的可移植性。同时,这些字符串函数只能处理ASCII字符集或兼容ASCII的字符集,如ISO-8859-1;在处理存在多字节字符的字符集,如UTF-8时,会产生一个警告,指出对字符串“长度”的计算是以字节而不是以Unicode字符为单位。非ASCII兼容字符集的字符串处理函数一般位于wchar.h
中。
Constants and types
Name | Notes |
---|---|
NULL |
macro expanding to the null pointer constant; that is, a constant representing a pointer value which is guaranteed not to be a valid address of an object in memory. |
size_t |
an unsigned integer type which is the type of the result of the sizeof operator.
|
Functions
Name | Notes |
---|---|
void *memcpy(void *dest, const void *src, size_t n);
|
copies n bytes between two memory areas; if there is overlap, the behavior is undefined |
void *memmove(void *dest, const void *src, size_t n);
|
copies n bytes between two memory areas; unlike with memcpy the areas may overlap
|
void *memchr(const void *s, char c, size_t n);
|
returns a pointer to the first occurrence of c in the first n bytes of s, or NULL if not found |
int memcmp(const void *s1, const void *s2, size_t n);
|
compares the first n characters of two memory areas |
void *memset(void *, int, size_t);
|
overwrites a memory area with a byte pattern |
char *strcat(char *dest, const char *src);
|
appends the string src to dest |
char *strncat(char *dest, const char *src, size_t n);
|
appends at most n characters of the string src to dest |
char *strchr(const char *, int);
|
locates a character in a string, searching from the beginning |
char *strrchr(const char *, int);
|
locates a character in a string, searching from the end |
int strcmp(const char *, const char *);
|
compares two strings lexicographically |
int strncmp(const char *, const char *, size_t);
|
compares up to the first n bytes of two strings lexicographically |
int strcoll(const char *, const char *);
|
compares two strings using the current locale's collating order |
char *strcpy(char *toHere, const char *fromHere);
|
copies a string from one location to another |
char *strncpy(char *toHere, const char *fromHere, size_t);
|
copies up to n bytes of a string from one location to another |
char *strerror(int);
|
returns the string representation of an error number e.g. errno (not thread-safe) |
size_t strlen(const char *);
|
finds the length of a C string |
size_t strspn(const char *s, const char *accept);
|
determines the length of the maximal initial substring of s consisting entirely of characters in accept |
size_t strcspn(const char *s, const char *reject);
|
determines the length of the maximal initial substring of s consisting entirely of characters not in reject |
char *strpbrk(const char *s, const char *accept);
|
finds the first occurrence of any character in accept in s |
char *strstr(const char *haystack, const char *needle);
|
finds the first occurrence of the string "needle" in the longer string "haystack". |
char *strtok(char *, const char *);
|
parses a string into a sequence of tokens; non-thread safe in the spec, non-reentrant |
size_t strxfrm(char *dest, const char *src, size_t n);
|
transforms src into a collating form, such that the numerical sort order of the transformed string is equivalent to the collating order of src. |
Extensions to ISO C
Name | Notes | Specification |
---|---|---|
void *memccpy(void *dest, const void *src, int c, size_t n
|
copies up to n bytes between two memory areas, which must not overlap, stopping when the byte c is found | UNIX 98? |
void *mempcpy(void *dest, const void *src, size_t n);
|
variant of memcpy returning a pointer to the byte following the last written byte
|
GNU |
errno_t strcat_s(char *s1, size_t s1max, const char *s2);
|
bounds-checked variant of strcat
|
ISO/IEC WDTR 24731 |
errno_t strcpy_s(char *s1, size_t s1max, const char *s2);
|
bounds-checked variant of strcpy
|
ISO/IEC WDTR 24731 |
char *strdup(const char *);
|
allocates and duplicates a string into memory | POSIX; originally a BSD extension |
int strerror_r(int, char *, size_t);
|
Puts the result of strerror() into the provided buffer in a thread-safe way. | POSIX:2001 |
char *strerror_r(int, char *, size_t);
|
Return strerror() in a thread-safe way. The provided buffer is used only if necessary (incompatible with POSIX version). | GNU |
size_t strlcat(char *dest, const char *src, size_t n);
|
bounds-checked variant of strcat
|
originally OpenBSD, now also FreeBSD, Solaris, Mac OS X |
size_t strlcpy(char *dest, const char *src, size_t n);
|
bounds-checked variant of strcpy
|
originally OpenBSD, now also FreeBSD, Solaris, Mac OS X |
char *strsignal(int sig);
|
by analogy to strerror , returns string representation of the signal sig (not thread safe)
|
BSDs, Solaris, Linux |
char *strtok_r(char *, const char *, char **);
|
thread-safe and reentrant version of strtok | POSIX |