当前位置:网站首页>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"

40511d864e6742dd912c84b80c6b6d68.gif

    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 .

2537a46fb4034f2793f8a5a481f50dc5.gif


  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 .

c1f8cce3a530463d98fd78597c15992e.png


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

9fbb061efb63461f93b8bf4965bdda69.png

Schematic diagram of two-dimensional array storage in memory :

c8e40cb9fb274befa7ddb6a0e4dfc46e.png


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 :

3856c7065dff4cc3b2ab75ae63eafea8.png

Schematic diagram of two-dimensional array out of bounds :

1ecd12d250e543b48335b25ecd4839d9.png


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 :

8087e9212d0146f090d62e21fdd8504d.png

Schematic diagram of pointer form :

d199fb23794f40faa67d03da311c7fce.png


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 :

ae9785bb98d04806b76d96d84cce469c.png

  Flow diagram of sequential bubble sorting : 

d8dcfe4c61c7498996ebee98a625f506.png

Code examples and partial analysis :

ba0884f777f24861a1e4675876a47dd4.png

  Code output result diagram :

ee8fd1540847479a86018f29687291b0.png


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 !

80b424c160bd43bbb0a115b9c15cfabe.gif


原网站

版权声明
本文为["Xiao Yang"]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071128351532.html