当前位置:网站首页>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 !
边栏推荐
- Server to server (S2S) event (adjust)
- Centso7 OpenSSL error Verify return code: 20 (unable to get local issuer certificate)
- flask session伪造之hctf admin
- Mongodb replication (replica set) summary
- [etc.] what are the security objectives and implementation methods that cloud computing security expansion requires to focus on?
- How did Guotai Junan Securities open an account? Is it safe to open an account?
- 1、深拷贝 2、call apply bind 3、for of for in 区别
- DID登陆-MetaMask
- How to make the new window opened by electorn on the window taskbar
- centso7 openssl 报错Verify return code: 20 (unable to get local issuer certificate)
猜你喜欢
[dark horse morning post] Huawei refutes rumors about "military master" Chen Chunhua; Hengchi 5 has a pre-sale price of 179000 yuan; Jay Chou's new album MV has played more than 100 million in 3 hours
Digital IC Design SPI
Ogre introduction
MongoDB内部的存储原理
10 张图打开 CPU 缓存一致性的大门
PACP学习笔记一:使用 PCAP 编程
Talk about pseudo sharing
1、深拷贝 2、call apply bind 3、for of for in 区别
About how appium closes apps (resolved)
Distributed transaction solution
随机推荐
Unity build error: the name "editorutility" does not exist in the current context
High end for 8 years, how is Yadi now?
提升树莓派性能的方法
Solve the cache breakdown problem
MongoDB 遇见 spark(进行整合)
分屏bug 小记
工具箱之 IKVM.NET 项目新进展
648. 单词替换 : 字典树的经典运用
JS function returns multiple values
[untitled]
JNA学习笔记一:概念
Enregistrement de la navigation et de la mise en service du robot ROS intérieur (expérience de sélection du rayon de dilatation)
Mongodb meets spark (for integration)
MongoDB复制(副本集)总结
RecyclerView的数据刷新
Ways to improve the performance of raspberry pie
学习突围2 - 关于高效学习的方法
ESP32 ① 编译环境
MySQL error 28 and solution
一文读懂数仓中的pg_stat