当前位置:网站首页>Deep understanding of array related problems in C language
Deep understanding of array related problems in C language
2022-07-07 13:31:00 【"Xiao Yang"】
Hello, everyone ! I'm Xiao Yang . Xiao Yang Ba C The common problems in arrays in language are summarized , I hope I can help you while facilitating your understanding and review .
List of questions :
problem 1: The difference between the two ways of writing character arrays ?
problem 2: Can a two-dimensional array be regarded as a one-dimensional array ?
problem 3: The storage relationship between one-dimensional array and two-dimensional array in memory ?
problem 4: The number of rows of a two-dimensional array can be omitted , The reason why the number of columns cannot be omitted ?
problem 5: One dimensional array and two-dimensional array cross-border access ?
problem 6: Array parameter transfer , Two ways of writing formal parameters ?
problem 7: Bubble sort to sort integer arrays in ascending order ?
Problem solving :
problem 1: Two ways to write character arrays ?
answer : because C There is no string type in the language , So use char Array instead of string type , There are two ways to define character arrays , Respectively : In single character form and string form .
Mode one : In single character form
To declare initialization of a character array that can store four valid values , Form the following :
char ch[4]={'w','o','r,'d'};
Mode two : In string form
To declare initialization of a character array that can store four valid values , Form the following :
char str[5]=''word";// The size of string array must be declared as 5, Because we should set aside str[4] Automatically store for the compiler '\0', If the declaration is initialized to :char str[4]="word", Report errors
Be careful :
1. When it is not full , For two arrays , The remaining valid positions are initialized as '\0'
2. When it's full , The last valid position of the character array is not necessarily assigned '\0', It can still be a valid value you want to store , But the last valid position of the string array must be '\0'
problem 2: Can a two-dimensional array be regarded as a one-dimensional array ?
answer : Sure , The two-dimensional array is originally regarded as a special one-dimensional array , It can be understood that a two-dimensional array is an array of one-dimensional arrays . Popular understanding is : Each row of the original two-dimensional array represents the elements in the special one-dimensional array .
problem 3: The storage relationship between one-dimensional array and two-dimensional array in memory ?
answer : The layout of one-dimensional array and two-dimensional array in memory is the same , That is, it is stored continuously in memory , Move from low address to high address ; However, there are some differences between the two forms of access , They are accessed in the form of one-dimensional array and two-dimensional array .
Schematic diagram of one-dimensional array storage in memory :
Schematic diagram of two-dimensional array storage in memory :
problem 4: The number of rows of a two-dimensional array can be omitted , The reason why the number of columns cannot be omitted ?
answer : Two dimensional arrays can omit rows . Because two-dimensional arrays are stored “ First, then ”, If you do not specify the number of columns , You can't know how many data to put in a row , As long as you know the number of columns , The platoon leader can know how many lines can be put in total .
problem 5: One dimensional array and two-dimensional array cross-border access ?
answer : The subscript of an array is range limited . The next stipulation of the array is from 0 At the beginning , If the array has n Elements , The subscript of the last element is n-1. So if the subscript of the array is less than 0, Or greater than n-1, The array is accessed out of bounds , Access beyond the legal space of the array . Rows and columns of two-dimensional arrays may also be out of bounds . and C The language itself does not check the bounds of array subscripts , The compiler does not necessarily report an error , But the compiler does not report an error , That doesn't mean the program is That's right. , So when programmers write code , You'd better do cross-border inspection yourself .
One dimensional array out of bounds diagram :
Schematic diagram of two-dimensional array out of bounds :
problem 6: Array parameter transfer , Two ways of writing formal parameters ?( And problems 7 Related to )
answer : How to write it 1: Array form How to write it 2: Pointer form
Sample questions 7 To specify both .
Schematic diagram of array form :
Schematic diagram of pointer form :
problem 7: Bubble sort to sort integer arrays in ascending order ?
answer : Bubble sorting core idea : Compare two adjacent elements .
The result of bubble sort is that a bubble sort brings a data to its final position .
Schematic diagram of bubble sorting principle :
Schematic diagram of the first bubble sorting process :
Flow diagram of sequential bubble sorting :
Code examples and partial analysis :
Code output result diagram :
Conclusion
friends , When you learn here , You should have harvested a lot !🥳🥳🥳 Xiao Yang will summarize other contents for you later , Constantly update high-quality content to help you , Progress together . come on. , Dream catcher ! Let's embrace a better tomorrow !
边栏推荐
- 迅为iTOP-IMX6ULL开发板Pinctrl和GPIO子系统实验-修改设备树文件
- clion mingw64中文乱码
- Cloud detection 2020: self attention generation countermeasure network for cloud detection in high-resolution remote sensing images
- JS中为什么基础数据类型可以调用方法
- 【等保】云计算安全扩展要求关注的安全目标和实现方式区分原则有哪些?
- 线程池拒绝策略最佳实践
- Scrapy教程经典实战【新概念英语】
- Read PG in data warehouse in one article_ stat
- About the problem of APP flash back after appium starts the app - (solved)
- Esp32 ① compilation environment
猜你喜欢
Navicat运行sql文件导入数据不全或导入失败
【学习笔记】zkw 线段树
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
OSI seven layer model
将数学公式在el-table里面展示出来
Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
PAcP learning note 1: programming with pcap
QQ的药,腾讯的票
1、深拷贝 2、call apply bind 3、for of for in 区别
About how appium closes apps (resolved)
随机推荐
PAcP learning note 1: programming with pcap
Milkdown 控件图标
SSRF漏洞file伪协议之[网鼎杯 2018]Fakebook1
【学习笔记】zkw 线段树
简单好用的代码规范
Signal strength (RSSI) knowledge sorting
Pcap learning notes II: pcap4j source code Notes
C语言数组相关问题深度理解
解决缓存击穿问题
JS判断一个对象是否为空
Read PG in data warehouse in one article_ stat
Mongodb meets spark (for integration)
[etc.] what are the security objectives and implementation methods that cloud computing security expansion requires to focus on?
Summary of import, export, backup and recovery of mongodb
DETR介绍
How to make join run faster?
线程池拒绝策略最佳实践
648. 单词替换 : 字典树的经典运用
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
How to make the new window opened by electorn on the window taskbar