当前位置:网站首页>C语言之字符串与字符数组的区别
C语言之字符串与字符数组的区别
2022-04-23 13:07:00 【云小逸】
1.字符串的定义:
(1)单个字符:
char ch='i';//单个字符的定义
(2)一维字符串数组:
char arr[]="love";(这种方法定义的一维字符串数组必须赋值)
char arr[4];(想内存申请创建可以存储3个字符的数组空间)
char arr[5]=”love”;(开辟5个字节的空间存放字符love,最后一个字节存放'\0'字符)
char arr[5]={'l','o',v','e'};(开辟5个字节的空间存放字符love,最后一个字节存放'\0'字符)
2.字符串长度
(1)sizeof(arr)是求该字符串的有多少字符,由于\0也是一个字符,所以数组实际长度加一;
(2)strlen(arr)因为strlen碰见\0就会停止,所以结果是数组的实际长度
3.字符串和字符数组的区别:
由于C语言中没有string关键字,所以不能定义字符串,只能用字符数组来表示字符串,也因此很多人认为两者是一个概念,笔者以前也是这样认为的,后来听到老师提过字符串和字符数组是有区别,因此我开始翻阅一些书籍,才了解了两者的区别,接下来请听我说:
(1)字符数组:
是定义一个字符数组,向内存申请的空间是数组里存放元素个数+1,向内存存放数组的字符元素
char arr1[] = { 'l','o','v','e' };
(2)字符串:
是定义一个字符数组,向内存申请的空间是数组里存放元素个数+1,向内存存放数组的字符元素+'\0',编译时编译器会自动在字符串的末尾添加此值,字符串是一个只读型字符型数组,不能够通过指针更改字符串内部数据
char arr2[] = "love";
可以简单的这么说:c语言的字符串由C的字符数组末尾加上'\0'变形而成。
#include<stdio.h>
int main(void)
{
char arr1[] = { 'l','o','v','e' };
char arr2[] = "love";
printf("arr1的长度 :%zu\n", sizeof(arr1));
printf("arr2的长度 :%zu\n", sizeof(arr2));
printf("arr1的长度 :%d\n", strlen(arr1));
printf("arr2的长度 :%d\n", strlen(arr2));
printf("arr1的内容 :%s\n",arr1);
printf("arr2的长度 :%s\n", arr2);
return 0;
}
注:第三行出现:arr1的长度 :36和第五行出现:arr1的内容 :love烫烫烫烫烫烫烫烫烫烫烫烫烫烫love的原因是strlen和printf函数均是遇到'\0'才停住,由于定义字符数组时没有加上'\0'导致'\0'位置未知,故出现此种情况,用我以前的一篇博客(初识C语言(1))做的图供大家理解:
版权声明
本文为[云小逸]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_68865259/article/details/124346496
边栏推荐
- Subscribe to Alibaba demo send business messages
- Conflict between Mui picker and drop-down refresh
- CVPR 2022 & ntire 2022 | the first transformer for hyperspectral image reconstruction
- AUTOSAR from introduction to mastery 100 lectures (87) - key weapon of advanced EEA - AUTOSAR and DDS
- Custom nail robot alarm
- These vscode extensions are my favorite
- Introduction to metalama 4 Use fabric to manipulate items or namespaces
- office2021安装包下载与激活教程
- uniapp image 引入本地图片不显示
- Temperature and humidity monitoring + timing alarm system based on 51 single chip microcomputer (C51 source code)
猜你喜欢
Wonderful review | the sixth issue of "source" - open source economy and industrial investment
Record Alibaba cloud server mining program processing
mysql支持ip访问
世界读书日:我想推荐这几本书
About the 'enum' enumeration type and structure.
STM32 is connected to the motor drive, the DuPont line supplies power, and then the back burning problem
three.js文字模糊问题
Design and manufacture of 51 single chip microcomputer solar charging treasure with low voltage alarm (complete code data)
8086 of x86 architecture
Kernel error: no rule to make target 'Debian / canonical certs pem‘, needed by ‘certs/x509_ certificate_ list‘
随机推荐
Mui + hbuilder + h5api simulate pop-up payment style
MySQL basic statement query
Mui wechat payment pit
解决虚拟机中Oracle每次要设置ip的问题
mysql支持ip访问
MySQL supports IP access
Important knowledge of network layer (interview, reexamination, term end)
nodejs + mysql 实现简单注册功能(小demo)
100 GIS practical application cases (34) - splicing 2020globeland30
Read the data in Presto through sparksql and save it to Clickhouse
The project file '' has been renamed or is no longer in the solution, and the source control provider associated with the solution could not be found - two engineering problems
安装nngraph
Pytorch: a pit about the implementation of gradreverselayer
4.22学习记录(你一天只做了水题是吗)
PC starts multiple wechat at one time
Embrace the new blue ocean of machine vision and hope to open a new "Ji" encounter for the development of digital economy
These vscode extensions are my favorite
将新增和编辑的数据同步更新到列表
mui 微信支付 排坑
(personal) sorting out system vulnerabilities after recent project development