当前位置:网站首页>014 C language foundation: C string
014 C language foundation: C string
2022-06-27 04:24:00 【Prison plan progress 50%】
List of articles
One : summary
stay C In language , Strings are actually empty characters \0 One dimensional character array at the end . therefore ,\0 Is used to mark the end of the string .
Null character (Null character) Also known as Terminator , abbreviation NULL, Is a value of 0 Control characters of ,\0 It's the escape character , Tell the compiler , This is not a character 0, It's an empty character .
For example, words “hello”char greeting[6] = {'h', 'e', 'l', 'l', 'o', ''};
Because there are more at the end "", So the size of the character array is bigger than the word "hello" One more character count of .
According to the array initialization rules , You can write the above sentence as the following :char greeting[] = "hello";
example :
#include <stdio.h>
int main(){
char greeting[6] = {
'h', 'e', 'l', 'l', 'o', '\0'};
printf("%s \n", greeting);
return 0;
}
The output is :hello
Two : String function
function | describe |
---|---|
strcpy(s1, s2); | Copy string s2 To string s1 |
strcat(s1, s2); | Connection string s2 To string s1 At the end of |
strlen(s1); | Return string s1 The length of |
strcmp(s1, s2); | If s1 and s2 It's the same , Then return to 0; If s1<s2 Then return less than 0; If s1>s2 Return greater than 0 |
strchr(s1, ch); | Return a pointer , Point to string s1 In the character ch Where it first appeared . |
strstr(s1, s2); | Return a pointer , Point to string s1 Middle string s2 Where it first appeared . |
example :
#include <stdio.h>
int main(){
char str1[12] = "hello";
/* Sure char str1[12] = "hello"; Can not be ,char str1[12]; str1 = "hello"; This is because it defines str1 After array , Then use the equal sign to give str1 assignment , This = No. requires that the left side is a variable , The array name defined is a constant */
char str2[12] = "world";
char str3[12] = "cc";
int len;
strcpy(str1, str3); // Copy str3 To str1
printf("strcpy(str3,str1): %s \n", str3);
/* The result here is ccc, Why? ? First understand str1[12]="hello"; Inside 12 It refers to the length of the array , And words hello Occupy 5 Length , Actually, it's followed by \0, All together 6 Length , this 6 The total length is called string length . stay C Language string , encounter \0 That is to say NULL Represents the end of the string . that , alike str3 The string in this array is actually :"cc\0", and str1:"hello\0", after strcpy after , Should become cc\0lo\0, first \0 Instead of l The location of . But I said before , stay C Language string , encounter \0 That is to say NULL Represents the end of the string . therefore , The result is cc */
strcat(str1, str2); // Connect str2 To str1 At the end of
printf("strcat(str1, str2): %s \n", str1);
len = strlen(str1); // After connection ,str1 The total length of , It doesn't contain \0
printf("strlen(str1): %d \n", len);
int num;
num = strcmp(str1, str2);
printf("srcmp(str1, str2): %d \n", num); // -1
int ch = 'w';
int *ptr;
ptr = strchr(str1, ch);
printf("%c The string after the start is %s \n", ch, ptr);
return 0;
}
result :
┌──(rootkali)-[~/Desktop/c_test]
└─# ./zifuchuan
strcpy(str3,str1): cc
strcat(str1, str2): ccworld
strlen(str1): 7
srcmp(str1, str2): -20
w The string after the start is world
strcpy The function does not check the bounds of the array , It is very easy to cause various buffer overflow vulnerabilities . These vulnerabilities are easy to exploit , And cause serious system problems . In the use of strcpy Function time , Be careful .
边栏推荐
- Kotlin compose implicitly passes the parameter compositionlocalprovider
- 006 C语言基础:C存储类
- 008 C语言基础:C判断
- 高等数学(第七版)同济大学 习题1-10 个人解答
- 【B站UP DR_CAN学习笔记】Kalman滤波3
- Cultural tourism light show breaks the time and space constraints and shows the charm of night tour in the scenic spot
- In a sense, the Internet has become an incubator and a parent
- 日志收集系统
- 如何让 EF Core 6 支持 DateOnly 类型
- PostgreSQL基础命令教程:创建新用户admin来访问PostgreSQL
猜你喜欢
Golang Hello installation environment exception [resolved]
PostgreSQL基础命令教程:创建新用户admin来访问PostgreSQL
Microservice system design -- unified authentication service design
MySQL development environment
fplan-Powerplan实例
Ldr6028 OTG data transmission scheme for mobile devices while charging
Microservice system design - service fusing and degradation design
百度飞桨“万有引力”2022首站落地苏州,全面启动中小企业赋能计划
卷积神经网络(CNN)网络结构及模型原理介绍
差点因为 JSON.stringify 丢了奖金...
随机推荐
Learn crypto from Buu (Zhou Geng)
Nignx configuring single IP current limiting
math_ Number set (number set symbol) and set theory
文旅灯光秀打破时空限制,展现景区夜游魅力
微服务系统设计——微服务调用设计
Kotlin compose custom compositionlocalprovider compositionlocal
Microservice system design -- service registration, discovery and configuration design
iOS开发:对于动态库共享缓存(dyld)的了解
PostgreSQL基础命令教程:创建新用户admin来访问PostgreSQL
MySQL development environment
Method of decoding iPhone certificate file
ERP需求和销售管理 金蝶
【C语言】关键字的补充
USB DRIVER
微服务系统设计——服务链路跟踪设计
微服务系统设计——分布式定时服务设计
深潜Kotlin协程(十五):测试 Kotlin 协程
Microservice system design -- distributed lock service design
010 C语言基础:C函数
733. image rendering