- C 标准库 —— string.h
- C 标准库 —— time.h
- C 标准库 —— limits.h
- C 标准库 —— stdio.h
- C 标准库—— stdlib.h(包括 rand srand 的实现)
- C 标准库—— <cmath>/<math.h>
- C 标准库—— assert.h
1. strlen()(与 sizeof())
遇\0
退出,它返回的字符串的长度是真实的长度。
strlen(str) + 1 == sizeof(str);
二者最为重要的区别在于:
- strlen 是函数调用(function call);
- sizeof 编译期确定(compile time);
2. strcpy()
把从 src 地址开始且含有 ‘\0’ 结束符的字符串复制到以 dest 开始的地址空间。
char* strcpy(char* des,const char* source)
{
char* r=des;
assert((des != NULL) && (source != NULL));
while((*des++ = *source++)!='\0');
return r;
}
这就要求 dst 的长度不少于 src 的长度;
3. strcpy vs memcpy
- memcpy() 是用来拷贝内存的,
- 头文件:<string.h>(malloc 所在的头文件则是:<stdlib.h>)
- strcpy() 是用来拷贝内容的,遇到’\0’就结束
- 头文件:<string.h>
4. 指定长度字符串的拷贝:strncpy
char* dst = NULL;
dst = (char*)malloc(sizeof(src));
strncpy(dst, src, sizeof(src));
// strncpy(dst, src, strlen(src)+1);
5. warning: incompatible implicit declaration of built-in function ‘memcpy’
为 memset/memcpy 等函数引入头文件, string.h/memory.h
char cache[250][250];
memset(cache, -1, sizeof(cache));
// 比 for 循环更为高效