目录
1 memchr()函数——在内存空间中搜索一个字符
函数概述:
memchr 函数扫描 s 指向的内存空间的前 n 个字节,找到第一个匹配字符 c 时停止操作。
函数原型:
#include <string.h>
...
void *memchr(const void *s, int c, size_t n);
参数解析:
参数 | 含义 |
---|---|
s | 指向目标内存空间 |
c | 指向目标字符 |
n | 指定最大扫描字节数 |
返回值:
如果找到该字符,则返回指向该字符的指针。
如果找不到该字符,返回 NULL。
示例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[13] = "Hello world!";
char *ptr = NULL;
ptr = (char *)memchr(str, 'd', 13);
if (ptr != NULL)
{
printf("找到字符d!\n");
}
else
{
printf("找不到字符d!\n");
}
return 0;
}
2 memcmp()函数——比较内存空间
函数概述:
memcmp 函数比较 s1 和 s2 指向的两个内存空间前 n 个字节,返回一个小于,等于或大于 0 的数表示 s1 小于、等于或大于 s2。
函数原型:
#include <string.h>
...
int memcmp(const void *s1, const void *s2, size_t n);
参数解析:
参数 | 含义 |
---|---|
s1 | 指向待比较的第一个内存空间 |
s2 | 指向待比较的第二个内存空间 |
n | 指定需要比较的字节个数 |
返回值:
返回一个整数表示两个内存空间的大小关系:
返回值 | 含义 |
---|---|
< 0 | 字符串 1 的字符小于字符串 2 对应位置的字符 |
0 | 两个字符串的内容完全一致 |
> 0 | 字符串 1 的字符大于字符串 2 对应位置的字符 |
示例:
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[13] = "Hello world!";
char str2[13] = "Hello world!";
if (memcmp(str1, str2, 13) == 0)
{
printf("两个内存空间完全一致!\n");
}
else
{
printf("两个内存空间不一致!\n");
}
return 0;
}
3 memcpy()函数——拷贝内存空间
函数概述:
memcpy 函数从 src 指向的内存空间拷贝 n 个字节到 dest 指向的内存空间。src 和 dest 指向的内存区域不能出现重叠,否则应该使用 memmove 函数。
memcpy 函数并不关心被复制的数据类型,只是逐字节地进行复制,这给函数的使用带来了很大的灵活性,可以面向任何数据类型进行复制。
函数原型:
#include <string.h>
...
void *memcpy(void *dest, const void *src, size_t n);
参数解析:
参数 | 含义 |
---|---|
dest | 指向目标内存空间 |
src | 指向源内存空间 |
n | 指定要拷贝到 dest 指向空间的前 n 个字节 |
返回值:
返回 dest 指向的内存空间。
示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char str[] = "I love China very much!";
char *ptr;
int length = sizeof(str);
printf("length = %d\n", length);
ptr = (char *)malloc(length * sizeof(char));
if (ptr == NULL)
{
exit(1);
}
memset(ptr, 0, length);
memcpy(ptr, str, length);
printf("%s\n", ptr);
return 0;
}
4 memmove()函数——拷贝内存空间
函数概述:
memmove 函数从 src 指向的内存空间拷贝 n 个字节到 dest 指向的内存空间。为了避免因为两个内存空间出现重叠而导致的悲剧,该函数会先将 src 指向的内存空间拷贝到一个临时的数组中,完了再从临时数组拷贝到目标内存空间。
函数原型:
#include <string.h>
...
void *memmove(void *dest, const void *src, size_t n);
参数解析:
参数 | 含义 |
---|---|
dest | 指向目标内存空间 |
src | 指向源内存空间 |
n | 指定要拷贝到 dest 指向空间的前 n 个字节 |
返回值:
返回 dest 指向的内存空间。
示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char str[] = "I love China very much!";
char *ptr;
int length = sizeof(str);
printf("length = %d\n", length);
ptr = (char *)malloc(length * sizeof(char));
if (ptr == NULL)
{
exit(1);
}
memset(ptr, 0, length);
memmove(ptr, str, length);
printf("%s\n", ptr);
return 0;
}
5 memset()函数——使用一个常量字节填充内存空间
函数概述:
memset 函数使用常量字节 c 填充 s 指向的前 n 个字节。
memset 函数按照字符数组的方式操作内存对象,其主要目的是提供一个高效的函数接口,通常用于初始化 malloc 函数申请的内存对象。
函数原型:
#include <string.h>
...
void *memset(void *s, int c, size_t n);
参数解析:
参数 | 含义 |
---|---|
s | 指向要操作的内存空间 |
c | 指定要填充的值 |
n | 指定要填充 s 指向空间的前 n 个字节 |
返回值:
返回 s 指向的内存空间。
示例:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM 100
int main(void)
{
int *ptr;
ptr = (int *)malloc(NUM * sizeof(int));
if (ptr == NULL)
{
exit(1);
}
memset(ptr, 0, NUM);
return 0;
}
6 strcat()函数——连接字符串
函数概述:
strcat 函数用于连接两个字符串。
将源字符串拷贝并连接到目标数组存放的字符串后边,此过程将覆盖第一个参数的结束符 '\0'。
两个参数的位置不应该重叠。
函数原型:
#include <string.h>
...
char *strcat(char *dest, const char *src);
参数解析:
参数 | 含义 |
---|---|
dest | 指向用于存放字符串的目标数组,它应该包含一个字符串,并且提供足够容纳连接后的总字符串长度的空间(包含结束符 '\0')。 |
src | 指向待连接的源字符串,该参数不应该与 dest 参数指向的位置发生重叠。 |
返回值:
返回值是指向目标字符串的指针。
示例:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100] = "I live in China";
char str2[] = "I love China very much!";
strcat(str1, " ,");
strcat(str1, str2);
printf("%s\n", str1);
return 0;
}
7 strcmp()函数——比较字符串
函数概述:
strcmp 函数用于比较两个字符串。
该函数从第一个字符开始,依次比较每个字符的 ASCII 码大小,直到发现两个字符不相等或抵达结束符('\0')为止。
函数原型:
#include <string.h>
...
int strcmp(const char *s1, const char *s2);
参数解析:
参数 | 含义 |
---|---|
s1 | 指向待比较的字符串 1 |
s2 | 指向待比较的字符串 2 |
返回值:
返回一个整数表示两个字符串的大小关系:
返回值 | 含义 |
---|---|
< 0 | 字符串 1 的字符小于字符串 2 对应位置的字符 |
0 | 两个字符串的内容完全一致 |
> 0 | 字符串 1 的字符大于字符串 2 对应位置的字符 |
示例:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[30] = "I love China very much!";
char str2[40] = "I love China very very much!";
if (!strcmp(str1,str2))
{
printf("两个字符串完全一致!\n");
}
else
{
printf("两个字符串不同!\n");
}
return 0;
}
8 strcpy()函数——拷贝字符串
函数概述:
strcpy 函数用于拷贝字符串,包含最后的结束符 '\0'。
为了避免溢出,必须确保用于存放的数组长度足以容纳待拷贝的字符串(注意:长度需要包含结束符 '\0')。
源字符串和目标数组的位置不应该重叠。
函数原型:
#include <string.h>
...
char *strcpy(char *dest, const char *src);
参数解析:
参数 | 含义 |
---|---|
dest | 指向用于存放字符串的目标数组 |
src | 指向待拷贝的源字符串 |
返回值:
返回值是指向目标字符串的指针。
示例:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Original string";
char str2[] = "New string";
char str3[100];
strcpy(str1, str2);
strcpy(str3, "Copy successful!");
printf("str1: %s\n", str1);
printf("str2: %s\n", str2);
printf("str3: %s\n", str3);
return 0;
}
9 strlen()函数——获取字符串的长度
函数概述:
strlen 函数用于返回指定字符串的长度。
C 语言字符串的长度取决于结束符('\0')的位置。
一个字符串的长度指的是从起始位置到结束符的字符个数(不包含结束符本身)。
函数原型:
#include <string.h>
...
size_t strlen ( const char * str );
参数解析:
参数 | 含义 |
---|---|
str | C 语言标准字符串 |
返回值:
如果该函数调用成功,返回值是指定字符串的长度(字符个数,不包含结束符 '\0')。
示例:
初学者很容易混淆字符串的长度和字符串数组的大小,例如:
#include <stdio.h>
#include <string.h>
int main ()
{
char str[100] = "I love China very much!";
printf ("该字符串的长度是%d\n",strlen(str));
printf ("该字符串数组的大小是%d\n",sizeof(str));
return 0;
}
上边代码定义了一个可以存放 100 个字符的数组,但 str 字符串数组只被初始化为包含 23 个字符的长度。因此,sizeof(str) 的结果(字符串数组的大小)是 100,而 strlen(str) 的结果(字符串长度)则是 23。
10 strncat()函数——连接字符串
函数概述:
strncat 函数用于拷贝源字符串中的 n 个字符到目标数组的字符串后边,并在末尾添加结束符 '\0'。
如果源字符串的长度小于 n,那么不会像 strncpy 函数那样使用 '\0' 进行填充(但结束符 '\0' 还是有的)。
另外,目标数组中的原有的字符串并不算在 n 中。
函数原型:
#include <string.h>
...
char *strncat(char *dest, const char *src, size_t n);
参数解析:
参数 | 含义 |
---|---|
dest | 指向用于存放字符串的目标数组,它应该包含一个字符串,并且提供足够容纳连接后的总字符串长度的空间(包含结束符 '\0') |
src | 指向待连接的源字符串,该参数不应该与 dest 参数指向的位置发生重叠 |
n | 指定待连接的源字符串的最大长度 |
返回值:
返回值是指向目标字符串的指针。
示例:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[30] = "I ";
char str2[20] = "love ";
char str3[20] = "China ";
char str4[20] = "very much!";
strncat(str1, str2, 5);
strncat(str1, str3, 6);
strncat(str1, str4, 10);
printf("str1: %s\n", str1);
return 0;
}
11 strncmp()函数——比较字符串
函数概述:
strncmp 函数用于比较两个字符串的前 n 个字符。
该函数从第一个字符开始,依次比较每个字符的 ASCII 码大小,发现两个字符不相等或抵达结束符('\0')为止,或者前 n 个字符完全一样,也会停止比较。
函数原型:
#include <string.h>
...
int strncmp(const char *s1, const char *s2, size_t n);
参数解析:
参数 | 含义 |
---|---|
s1 | 指向待比较的字符串 1 |
s2 | 指向待比较的字符串 2 |
n | 指定待比较的字符数 |
返回值:
返回一个整数表示两个字符串的关系:
返回值 | 含义 |
---|---|
< 0 | 字符串 1 的字符小于字符串 2 对应位置的字符 |
0 | 两个字符串的内容完全一致 |
> 0 | 字符串 1 的字符大于字符串 2 对应位置的字符 |
示例:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "I love China very much!";
char str2[] = "I love China very very much!";
if (!strncmp(str1, str2, 18))
{
printf("str1和str2前18个字符相同!\n");
}
else
{
printf("str1和str2前18个字符不同!\n");
}
return 0;
}
12 strncpy()函数——拷贝字符串
函数概述:
和 strcpy 函数一样,strncpy(dest, src, n) 函数将拷贝源字符串的 n 个字符到目标数组中。如果源字符串的长度小于 n,那么就用 '\0' 填充额外的空间。如果源字符串的长度大于或等于 n,那么只有 n 个字符被拷贝到目标数组中(注意:这样的话将不会以结束符 '\0' 结尾)。
为了使该函数更“安全”,建议使用 dest[sizeof(dest) - 1] = '\0'; 语句确保目标字符串是以 '\0' 结尾。
源字符串和目标数组的位置不应该重叠。
函数原型:
#include <string.h>
...
char *strncpy(char *dest, const char *src, size_t n);
参数解析:
参数 | 含义 |
---|---|
dest | 指向存放字符串的目标数组 |
src | 指向待拷贝的源字符串 |
n | 指定拷贝的最大长度 |
返回值:
返回值是指向目标字符串的指针。
示例:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "I love China very much!";
char str2[40];
char str3[40];
strncpy(str2, str1, sizeof(str2));
strncpy(str3, str2, 12);
str3[12] = '\0';
printf("%s\n", str1);
printf("%s\n", str2);
printf("%s\n", str3);
return 0;
}