当前位置:网站首页>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 函数时,要小心谨慎。
边栏推荐
- Further exploration of handler (Part 2) (the most complete analysis of the core principles of handler)
- 乐得瑞LDR6035 USB-C接口设备支持可充电可OTG传输数据方案。
- resnet152 辣椒病虫害图像识别1.0
- 高等数学(第七版)同济大学 习题1-10 个人解答
- 2021:passage retrieval for outside knowledgevisual question answering
- MySql的开发环境
- Description of replacement with STM32 or gd32
- ERP demand and sales management Kingdee
- Servlet and JSP final review examination site sorting 42 questions and 42 answers
- Network structure and model principle of convolutional neural network (CNN)
猜你喜欢

How to make ef core 6 support dateonly type

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

2021:Beyond Question-Based Biases:Assessing Multimodal Shortcut Learning in Visual Question Answeri

Cultural tourism light show breaks the time and space constraints and shows the charm of night tour in the scenic spot

Implementation of window encryption shell

promise源码-class版本【三、Promise源码】【代码详细注释/测试案例完整】

2021:AdaVQA: Overcoming Language Priors with Adapted Margin Cosine Loss∗自适应的边缘余弦损失解决语言先验

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

Learn crypto from Buu (Zhou Geng)

ERP需求和销售管理 金蝶
随机推荐
iOS开发:对于动态库共享缓存(dyld)的了解
渗透测试-文件上传/下载/包含
How can e-commerce products be promoted and advertised on Zhihu?
733. image rendering
缓存综合项目--秒杀架构
Static timing analysis OCV and time derive
Method of decoding iPhone certificate file
文旅夜游|以沉浸式视觉体验激发游客的热情
静态时序分析-OCV和time derate
jmeter将上一个请求的结果作为下一个请求的参数
Nignx configuring single IP current limiting
Building lightweight target detection based on mobilenet-yolov4
从某种意义来讲,互联网业已成为了一个孵化器,一个母体
math_ Number set (number set symbol) and set theory
Network structure and model principle of convolutional neural network (CNN)
Common sense of Apple's unique map architecture
Description of replacement with STM32 or gd32
2016Analyzing the Behavior of Visual Question Answering Models
Nestjs environment variable configuration to solve the problem of how to inject services into interceptors
Usage knowledge of mobile phones in new fields