String Basics in CC语言字符串基础
A C string is a character array terminated by the null character '\0'. This sentinel is fundamental to C string handling.
在 C 语言中,字符串是以null 终止符('\0')结尾的字符数组。这是 C 字符串处理的核心特征。
String Memory Representation:字符串内存表示:
"Hello" 在内存中的表示:
+------+------+------+------+------+------+
| 'H' | 'e' | 'l' | 'l' | 'o' | '\0' |
+------+------+------+------+------+------+
地址0 地址1 地址2 地址3 地址4 地址5
char str1[] = "Hello";
char str2[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char *str3 = "World";
⚠️ Common String Pitfalls常见字符串错误
- Buffer overflow: string exceeds allocated size缓冲区溢出: 字符串长度超过分配空间
- Missing terminator: forgetting '\\0' causes UB缺少终止符: 忘记添加'\\0'导致未定义行为
- Wild pointer: using freed string memory野指针: 操作已经释放的字符串内存
String Length Clamping字符串长度限制
Clamping string length is a key Week 1 practice: safely truncate overly long strings.
字符串长度限制是 Week 1 的重要练习:需要安全地截断过长的字符串。
void clamp_string(char *string, int limit) {
int length = 0;
while (string[length] != '\0') {
length++;
}
if (length >= limit) {
string[limit] = '\0';
}
}
🎯 字符串限制实例
int main() {
char text[] = "Hello, World!";
printf("原字符串: %s\n", text);
clamp_string(text, 5);
printf("限制后: %s\n", text);
return 0;
}
Filename Suffix Handling文件后缀处理
Handling filenames is a common string task; here we remove the file extension:处理文件名是字符串操作的常见应用,我们需要移除文件扩展名:
#include
#include
void remove_file_suffix(char *filename) {
int length = strlen(filename);
int dot_position = -1;
for (int i = length - 1; i >= 0; i--) {
if (filename[i] == '.') {
dot_position = i;
break;
}
}
if (dot_position > 0) {
filename[dot_position] = '\0';
}
}
💡 字符串函数库
The C standard library offers many string functions:C标准库提供了丰富的字符串处理函数:
- strlen(): get lengthstrlen(): 获取字符串长度
- strcpy(): copy stringstrcpy(): 复制字符串
- strcat(): concatenatestrcat(): 连接字符串
- strcmp(): comparestrcmp(): 比较字符串
- strchr(): find charstrchr(): 查找字符
- strstr(): find substringstrstr(): 查找子串
String Concatenation字符串组合
Dynamically creating a new string is an important skill:动态创建新字符串是字符串处理的重要技能:
void strip_newline(char *sentence) {
int length = strlen(sentence);
if (length > 0 && sentence[length - 1] == '\n') {
sentence[length - 1] = '\0';
}
}
char *combine_sentences(char *sentence1, char *sentence2) {
strip_newline(sentence1);
strip_newline(sentence2);
int len1 = strlen(sentence1);
int len2 = strlen(sentence2);
char *combined = (char *)malloc(len1 + len2 + 1);
if (combined == NULL) {
return NULL;
}
strcpy(combined, sentence1);
strcat(combined, sentence2);
return combined;
}
⚠️ 内存安全
- Always check malloc return value总是检查malloc的返回值
- Ensure string ends with '\\0'确保字符串以'\\0'结尾
- Avoid buffer overflows避免缓冲区溢出
- Free dynamically allocated memory记得释放动态分配的内存
Safe String Input安全字符串输入
Use fgets to safely read input and avoid buffer overflows:使用fgets安全地读取用户输入,避免缓冲区溢出:
#define MAX_LENGTH 100
int main() {
char filename[MAX_LENGTH];
printf("输入文件名: ");
if (fgets(filename, MAX_LENGTH, stdin) != NULL) {
strip_newline(filename);
printf("处理后的文件名: %s\n", filename);
remove_file_suffix(filename);
printf("无后缀文件名: %s\n", filename);
}
return 0;
}
💡 fgets vs scanf
- fgets(): safe, bounded readfgets(): 安全,可以指定最大读取长度
- scanf(): unsafe, may overflowscanf(): 不安全,容易导致缓冲区溢出
- gets(): dangerous, deprecatedgets(): 危险,已废弃,绝对不要使用
Summary总结
String handling is a fundamental C skill. In this module, you learned:字符串处理是C语言编程的基础技能,本周我们学习了:
- String Basics: importance of null terminator字符串基础:null终止符的重要性
- Safety: avoid buffer overflows安全操作:避免缓冲区溢出
- Length Limits: safe truncation长度限制:安全截断字符串
- File Handling: remove suffixes文件处理:移除文件后缀
- Dynamic Concatenation: create new strings动态组合:创建新字符串
- Memory Management: allocate and free safely内存管理:安全分配和释放
🚀 下一步学习
After mastering strings, you’ve covered most of Week 1. Next, learn arrays to complete the skill set.掌握了字符串处理后,我们已经完成了Week 1的大部分内容。接下来可以学习数组操作来完善技能体系。