当前位置:网站首页>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 !
边栏推荐
猜你喜欢
Final review notes of single chip microcomputer principle
QQ的药,腾讯的票
Scrapy教程经典实战【新概念英语】
Getting started with cinnamon applet
自定义线程池拒绝策略
About the problem of APP flash back after appium starts the app - (solved)
高端了8年,雅迪如今怎么样?
How far can it go to adopt a cow by selling the concept to the market?
My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
Ways to improve the performance of raspberry pie
随机推荐
[QNX hypervisor 2.2 user manual]6.3.4 virtual register (guest_shm.h)
Solve the cache breakdown problem
Some principles of mongodb optimization
[learning notes] agc010
Server to server (S2S) event (adjust)
User management summary of mongodb
[untitled]
MongoDB优化的几点原则
Clion mingw64 Chinese garbled code
Japanese government and enterprise employees got drunk and lost 460000 information USB flash drives. They publicly apologized and disclosed password rules
Shell batch file name (excluding extension) lowercase to uppercase
[learning notes] zkw segment tree
My "troublesome" subordinates after 00: not bad for money, against leaders, and resist overtime
ESP32构解工程添加组件
MongoDB 遇见 spark(进行整合)
Data refresh of recyclerview
MySQL error 28 and solution
迅为iTOP-IMX6ULL开发板Pinctrl和GPIO子系统实验-修改设备树文件
分屏bug 小记
记一次 .NET 某新能源系统 线程疯涨 分析