当前位置:网站首页>The difference between string and character array in C language
The difference between string and character array in C language
2022-04-23 13:11:00 【Yun Xiaoyi】
1. character string The definition of :
(1) Single character :
char ch='i';// Definition of a single character
(2) One dimensional string array :
char arr[]="love";( The one-dimensional string array defined by this method must be assigned )
char arr[4];( If you want to create a memory application, you can store 3 An array space of characters )
char arr[5]=”love”;( open up 5 Bytes of space to store characters love, The last byte stores '\0' character )
char arr[5]={'l','o',v','e'};( open up 5 Bytes of space to store characters love, The last byte stores '\0' character )
2. String length
(1)sizeof(arr) Is to find the number of characters in the string , because \0 It's also a character , So add one to the actual length of the array ;
(2)strlen(arr) because strlen meet \0 Will stop , So the result is the actual length of the array
3. The difference between string and character array :
because C Not in the language string keyword , So you can't define a string , Only character arrays can be used to represent strings , Therefore, many people think that the two are the same concept , I used to think so , Later, I heard the teacher mention that there is a difference between string and character array , So I started flipping through some books , Just to understand The difference between the two , Next, please listen to me :
(1) A character array :
Is to define a character array , The space requested from memory is the number of elements in the array +1, Store the character elements of the array into memory
char arr1[] = { 'l','o','v','e' };
(2) character string :
Is to define a character array , The space requested from memory is the number of elements in the array +1, Store the character elements of the array into memory +'\0', At compile time, the compiler will automatically add this value to the end of the string , A string is a Read only character array , You cannot change the internal data of the string through the pointer
char arr2[] = "love";
Simply put :c The language string consists of C Add... At the end of the character array '\0' Deformed into .
#include<stdio.h>
int main(void)
{
char arr1[] = { 'l','o','v','e' };
char arr2[] = "love";
printf("arr1 The length of :%zu\n", sizeof(arr1));
printf("arr2 The length of :%zu\n", sizeof(arr2));
printf("arr1 The length of :%d\n", strlen(arr1));
printf("arr2 The length of :%d\n", strlen(arr2));
printf("arr1 The content of :%s\n",arr1);
printf("arr2 The length of :%s\n", arr2);
return 0;
}
notes : The third line appears :arr1 The length of :36 And the fifth line appears :arr1 The content of :love Scald, scald, scald, scald love The reason is that strlen and printf All functions encounter '\0' Just stop , Because... Is not added when defining the character array '\0' Lead to '\0' Location unknown , So this happens , Use my previous blog ( First time to know C Language (1)) Make a picture for everyone to understand :
版权声明
本文为[Yun Xiaoyi]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231307069263.html
边栏推荐
- Pyqt5 store opencv pictures into the built-in sqllite database and query
- nodeJs + websocket 循环小案例
- Nodejs + websocket cycle small case
- mui 微信支付 排坑
- AUTOSAR from introduction to mastery 100 lectures (50) - AUTOSAR memory management series - ECU abstraction layer and MCAL layer
- mui + hbuilder + h5api模拟弹出支付样式
- Scons build embedded ARM compiler
- 9419页最新一线互联网Android面试题解析大全
- office2021安装包下载与激活教程
- "Play with Lighthouse" lightweight application server self built DNS resolution server
猜你喜欢
超40W奖金池等你来战!第二届“长沙银行杯”腾讯云启创新大赛火热来袭!
JMeter operation redis
普通大学生如何拿到大厂offer?敖丙教你一招致胜!
Important knowledge of transport layer (interview, retest, final)
Jupiter notebook installation
web三大组件之Servlet
你和42W奖金池,就差一次“长沙银行杯”腾讯云启创新大赛!
Free and open source agricultural Internet of things cloud platform (version: 3.0.1)
Nodejs + Mysql realize simple registration function (small demo)
Design and manufacture of 51 single chip microcomputer solar charging treasure with low voltage alarm (complete code data)
随机推荐
Imx6ull QEMU bare metal tutorial 2: usdhc SD card
Summary of JVM knowledge points - continuously updated
uniapp image 引入本地图片不显示
AUTOSAR from introduction to mastery 100 lectures (51) - AUTOSAR network management
Brief introduction of asynchronous encapsulation interface request based on uniapp
mui 关闭其他页面,只保留首页面
HQL find the maximum value in a range
【微信小程序】flex布局使用记录
(个人)最近项目开发后存在的系统漏洞整理
1130 - host XXX is not allowed to connect to this MySQL server error in Navicat remote connection database
Navicat远程连接数据库 出现 1130- Host xxx is not allowed to connect to this MySQL server错误
普通大学生如何拿到大厂offer?敖丙教你一招致胜!
Customize classloader and implement hot deployment - use loadclass
three. JS text ambiguity problem
decast id.var measure. Var data splitting and merging
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
AUTOSAR from introduction to mastery 100 lectures (52) - diagnosis and communication management function unit
【官宣】长沙软件人才实训基地成立!
Jupiter notebook installation
基于uniapp异步封装接口请求简介