当前位置:网站首页>014 C语言基础:C字符串
014 C语言基础:C字符串
2022-06-27 04:04:00 【入狱计划进度50%】
一:概述
在 C 语言中,字符串实际上是使用空字符 \0 结尾的一维字符数组。因此,\0 是用于标记字符串的结束。
空字符(Null character)又称结束符,缩写 NULL,是一个数值为 0 的控制字符,\0 是转义字符,意思是告诉编译器,这不是字符 0,而是空字符。
比如单词“hello”char greeting[6] = {'h', 'e', 'l', 'l', 'o', ''};
由于在末尾多了个"",所以字符数组的大小比单词"hello"的字符数多一个。
依据数组初始化规则,可以把上面的语句写成以下语句:char greeting[] = "hello";
实例:
#include <stdio.h>
int main(){
char greeting[6] = {
'h', 'e', 'l', 'l', 'o', '\0'};
printf("%s \n", greeting);
return 0;
}
结果输出为:hello
二:字符串函数
| 函数 | 描述 |
|---|---|
| strcpy(s1, s2); | 复制字符串s2到字符串s1 |
| strcat(s1, s2); | 连接字符串s2到字符串s1的末尾 |
| strlen(s1); | 返回字符串s1的长度 |
| strcmp(s1, s2); | 如果s1和s2是相同的,则返回0;如果s1<s2则返回小于0;如果s1>s2则返回大于0 |
| strchr(s1, ch); | 返回一个指针,指向字符串s1中字符ch的第一次出现的位置。 |
| strstr(s1, s2); | 返回一个指针,指向字符串s1中字符串s2的第一次出现的位置。 |
实例:
#include <stdio.h>
int main(){
char str1[12] = "hello";
/* 可以 char str1[12] = "hello"; 不可以,char str1[12]; str1 = "hello"; 这是因为定义了str1数组之后,然后再用等于号给str1赋值,这个=号要求左边是变量,而定义的数组名是常量 */
char str2[12] = "world";
char str3[12] = "cc";
int len;
strcpy(str1, str3); // 复制str3到str1
printf("strcpy(str3,str1): %s \n", str3);
/* 这里的结果是ccc,为什么呢? 先了解str1[12]="hello";这里面的12指的是这个数组的长度,而单词hello占5个长度,其实后面还跟着\0,一共占用6个长度, 这6个长度合起来称之为字符串长度。在C语言的字符串中,遇到\0也就是NULL代表着这个字符串的结束。 那么,同样的str3这个数组里面的字符串其实是:"cc\0",而str1:"hello\0", 经过strcpy之后,应该变成cc\0lo\0,第一个\0代替了l的位置。但是前面说过,在C语言的字符串中,遇到\0也就是NULL代表着这个字符串的结束。 所以,结果是cc */
strcat(str1, str2); // 连接str2到str1的末尾
printf("strcat(str1, str2): %s \n", str1);
len = strlen(str1); // 连接后,str1的总长度,不包含\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开始之后的字符串是 %s \n", ch, ptr);
return 0;
}
结果:
┌──(rootkali)-[~/Desktop/c_test]
└─# ./zifuchuan
strcpy(str3,str1): cc
strcat(str1, str2): ccworld
strlen(str1): 7
srcmp(str1, str2): -20
w开始之后的字符串是 world
strcpy 函数由于不对数组边界进行检查,而非常容易造成各种缓冲区溢出的漏洞。这些漏洞很容易被利用,而造成严重的系统问题。在使用 strcpy 函数时,要小心谨慎。
边栏推荐
- 2022-06-26: what does the following golang code output? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { type
- 从某种意义来讲,互联网业已成为了一个孵化器,一个母体
- 如何系统学习LabVIEW?
- IDEA中好用的插件
- Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?
- Fastdds server records - Translation-
- 日志收集系统
- 2019LXMERT:Learning Cross-Modality Encoder Representations from Transformers
- MySql的开发环境
- 真xx相来了?测试/开发程序员为什么不愿意加班,这是个疯狂的状态......
猜你喜欢

jmeter将上一个请求的结果作为下一个请求的参数

MySQL development environment

There are two problems when Nacos calls microservices: 1 Load balancer does not contain an instance for the service 2. Connection refused

静态时序分析-OCV和time derate

MATLAB | 基于分块图布局的三纵坐标图绘制

渗透测试-文件上传/下载/包含

2021:Passage Retrieval for Outside-KnowledgeVisual Question Answering通道检索的外部知识视觉问答

Why does C throw exceptions when accessing null fields?

nignx配置单ip限流

Network structure and model principle of convolutional neural network (CNN)
随机推荐
面试-01
跟着BUU学习Crypto(周更)
SAI钢笔工具如何使用,入门篇
语义化版本 2.0.0
Basic functions of promise [IV. promise source code]
Promise [II. Promise source code] [detailed code comments / complete test cases]
PostgreSQL basic command tutorial: create a new user admin to access PostgreSQL
Building lightweight target detection based on mobilenet-yolov4
Stack overflow vulnerability
2022-06-26: what does the following golang code output? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { type
手机新领域用法知识
jmeter分布式压测
Promise source code class version [III. promise source code] [detailed code comments / complete test cases]
2021:Beyond Question-Based Biases:Assessing Multimodal Shortcut Learning in Visual Question Answeri
Kotlin compose custom compositionlocalprovider compositionlocal
Static timing analysis OCV and time derive
Matlab | drawing of three ordinate diagram based on block diagram layout
Log collection system
文旅灯光秀打破时空限制,展现景区夜游魅力
resnet152 辣椒病虫害图像识别1.0