当前位置:网站首页>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
边栏推荐
- FatFs FAT32 learning notes
- [untitled] make a 0-99 counter, P1 7 connected to key, P2 connected to nixie tube section, common anode nixie tube, P3 0,P3. 1. Connect the nixie tube bit code. Each time you press the key, the nixie
- 基于uniapp异步封装接口请求简介
- Golang implements MD5, sha256 and bcrypt encryption
- 你和42W奖金池,就差一次“长沙银行杯”腾讯云启创新大赛!
- Free and open source intelligent charging pile SaaS cloud platform of Internet of things
- Custom nail robot alarm
- Summary of JVM knowledge points - continuously updated
- Temperature and humidity monitoring + timing alarm system based on 51 single chip microcomputer (C51 source code)
- 【行走的笔记】
猜你喜欢
【官宣】长沙软件人才实训基地成立!
Free and open source charging pile Internet of things cloud platform
Important knowledge of network layer (interview, reexamination, term end)
AUTOSAR from introduction to mastery 100 lectures (52) - diagnosis and communication management function unit
ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到
Custom nail robot alarm
Imx6ull QEMU bare metal tutorial 1: GPIO, iomux, I2C
9419 page analysis of the latest first-line Internet Android interview questions
Kernel error: no rule to make target 'Debian / canonical certs pem‘, needed by ‘certs/x509_ certificate_ list‘
LeetCode_DFS_中等_695.岛屿的最大面积
随机推荐
[notes de marche]
FatFs FAT32 learning notes
Go iris framework implements multi service Demo: start (listen to port 8084) service 2 through the interface in service 1 (listen to port 8083)
4.22 study record (you only did water problems in one day, didn't you)
ECDSA signature verification principle and C language implementation
"Play with Lighthouse" lightweight application server self built DNS resolution server
JDBC connection pool
Mui close other pages and keep only the first page
Mui + hbuilder + h5api simulate pop-up payment style
PC starts multiple wechat at one time
Proteus 8.10 installation problem (personal test is stable and does not flash back!)
Conflict between Mui picker and drop-down refresh
Design of body fat detection system based on 51 single chip microcomputer (51 + OLED + hx711 + US100)
Read the data in Presto through sparksql and save it to Clickhouse
(1) Openjuterpyrab comparison scheme
AUTOSAR from introduction to mastery 100 lectures (50) - AUTOSAR memory management series - ECU abstraction layer and MCAL layer
安装nngraph
EMMC / SD learning notes
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
Uniapp image import local image not displayed