string.h

Материал из Википедии — свободной энциклопедии
Это старая версия этой страницы, сохранённая Mathaddict (обсуждение | вклад) в 16:14, 12 июля 2009 ( Новая страница: «{{Стандартная библиотека Си}} {{lowercase|title=signal.h}} '''string.h''' - заголовочный файл, опре...»). Она может серьёзно отличаться от текущей версии.
(разн.) ← Предыдущая версия | Текущая версия (разн.) | Следующая версия → (разн.)
Перейти к навигации Перейти к поиску
Стандартная библиотека
языка программирования С

string.h - заголовочный файл, определенный в Стандартной библиотеке, содержащий функции для работы с нуль-терминированной строками и различными функциями работы с памятью

Функции объявленные в string.h широко используются, т.к. являясь частью стандартной библиотеки, они гарантированно работают на всех платформах, поддерживающих Си. Однако, существуют некоторые потенциальные проблемы с безопасностью, такие как переполнение буфера, что побуждает в пользу выбора более безопасных, возможно менее переносимых вариантов. Кроме этого, строковые функции работают только с набором символов ASCII или его совместимыми расширениями, такими как ISO-8859-1; многобайтовые кодировки такие как UTF-8 будут работать, с отличием, что "длина" строки будет определяться как число байтов, а не число символов Юникода, которым они соотвествуют. Несовместимые с ASCII строки обычно обрабатываются кодом описанным в wchar.h.

Константы и типы

Name Notes
NULL расширяется в null pointer; то есть, значение, которое гарантированное не является валидным адресом объекта в памяти.
size_t беззнаковое целое, имеющее тот же тип, что и результат оператораsizeof.

Функции

Name Notes
void *memcpy(void *dest, const void *src, size_t n); copies n bytes between two memory areas, which must not overlap
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 *, const char *, size_t); 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 numerically
int strncmp(const char *, const char *, size_t); compares up to the first n bytes of two strings lexigraphically
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
char *strdup(const char *); allocates and duplicates a string into memory POSIX; originally a BSD extension
errno_t strcpy_s(char *restrict s1, rsize_t s1max, const char *restrict s2); bounds-checked variant of strcpy ISO/IEC WDTR 24731
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
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?
int *strerror_r(int, char *, size_t); returns the string representation of an error number e.g. errno (thread-safe; some differences in semantics between GNU and XSI/POSIX) GNU, POSIX
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 *strtok_r(char *, const char *, char **); thread-safe version of strtok POSIX
char *strsignal(int sig); by analogy to strerror, returns string representation of the signal sig (not thread safe) BSDs, Solaris, Linux