当前位置:网站首页>Description of string
Description of string
2022-06-12 09:00:00 【Grilled fish of huangmanfen】
There are often some short sentences in our daily life , We are C These short sentences are called strings in the language , But in C There is no string type in the language , So we use character arrays to generalize a string .
First , Let's introduce A character array The concept of
We will 'a','b','c','0' Data types enclosed in single quotation marks are called character types , For a string, it is composed of one character, so for a string , There should be a group like generalizing a large character type to define it , We call it a character array . How to use it is as follows
char a1[] = "abcdefg";// ad locum abcdef For a string
char a2[] = {'a','b','c','d','e','f','g'};// ad locum abcdef Also a string In the previous code, we will use the string abcdef Stored in a character array , The writing form of character array is very similar to that of character type , That is, in the character type char Add a [ ], Here we can call it Character array type .
The above is a rough understanding of the string
Here we should have more questions and thoughts , Such as , What is the difference between the two forms of writing in the above code ?
Before making this clear, we need to understand array types carefully , See the link below :
About arrays - Basics _m0_59725109 The blog of -CSDN Blog
- Let's introduce a function - strlen
- strlen It's a library function , Included in <string.h> in , Only used to calculate the number of characters in a string , Be careful : Is only applicable to strings , It works by counting back from the first element of the string , Until I find '\0'
We use stlen To calculate the number of characters in the above two writing methods
char a1[] = "abcdefg";// ad locum abcdef For a string
char a2[] = { 'a','b','c','d','e','f','g' };// ad locum abcdef Also a string
printf("a1 = %d\n", strlen(a1));
printf("a2 = %d\n", strlen(a2));Get the results :

We find that in these two forms of writing strlen Calculation The number of characters is inconsistent , adopt strlen We can infer that These two strings are '\0' The location is different
Here are the results of debugging the code monitoring window :

It's not hard to find arrays a1 And array a2 The difference between the elements of a1 Than a2 One more. '\0' Elements , And it appears at the end of the array , We got strlen Compute string a1,a2 The reasons for the different results a1 Look back at the first element of the string '\0', Until I find '\0', remove '\0' After a total of 7 Elements , The result is 7, Because of the string a2 nothing '\0', therefore strlen Will always look back , Until I find '\0' until , So the result is random .
- We can also pass sizeof To test the results
char a1[] = "abcdefg";// ad locum abcdef For a string
char a2[] = { 'a','b','c','d','e','f','g' };// ad locum abcdef Also a string
printf("a1 = %d\n", sizeof(a1));
printf("a2 = %d\n", sizeof(a2));sizeof The size of the data is calculated , A character type is 1 Bytes , And the array mentioned above a1 Ratio array a2 One more '\0' combination , Array a1, Array a2 The size of should be 8,7
The code runs as follows :

边栏推荐
- Notes used by mqtt (combined with source code)
- 抓取屏幕与毛玻璃效果
- 【sklearn学习】LightGBM
- 最少换乘次数
- RuntimeError:Input and parameter tensors are not at the same device, found input tensor at cuda:0 an
- (十五) TweenRunner
- Dynamic segment tree leetcode six hundred and ninety-nine
- About weights exercise
- node示例后台搭建
- ISCSI详解(五)——ISCSI客户端配置实战
猜你喜欢

Background attribute compound writing

最少换乘次数

List < string > sort

Background color translucent

Application method of new version UI of idea + use method of non test qualification and related introduction

Introduction to applet cloud development -- questionnaire evaluation applet practice (7)

解压缩zip文件的工具类

Engineers learn music theory (I) try to understand music

2022 safety officer-c certificate special operation certificate examination question bank and simulation examination
![[open source project] easycmd command graphical software](/img/7b/4b4d7059a5dd520f3491cc376f41fc.jpg)
[open source project] easycmd command graphical software
随机推荐
第四章-第一个程序
Knee joint
Can you migrate backwards before the first migration in the south- Can you migrate backwards to before the first migration in South?
Random acquisition of 4-digit non repeated verification code
POI library update excel picture
Priority issues
128. 最长连续序列-哈希表
(十二)交互组件Selectable
《MATLAB 神经网络43个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测
MySQL learning records -- III. MySQL query statements
[data storage] storage of floating point data in memory
[advanced pointer I] character array & array pointer & pointer array
[character set 9] will GBK be garbled when copied to unicode?
清华大学数据挖掘笔记(一)
Knowledge points of 2022 system integration project management engineer examination: project cost management
抓取屏幕与毛玻璃效果
剑指 Offer II 016. 不含重复字符的最长子字符串-滑动窗口
2022 simulated examination platform operation of high voltage electrician work license question bank
[computer use] how to change a computer disk into a mobile disk?
Use NVM to dynamically adjust the nodejs version to solve the problem that the project cannot be run and packaged because the node version is too high or too low