当前位置:网站首页>Embedded-c Language-4
Embedded-c Language-4
2022-07-05 17:05:00 【Orange peel does not stop school】
One 、 Array
1.1 Computer programs actually play with memory , To play memory, you have to allocate it first , At present, there is only one way to allocate memory : Defining variables .
for example : int a;
int b;
int c; ........
Data types are consistent , And a large number of distribution , This allocation method will make the code extremely cumbersome .
ask : Is there a way to solve such problems : It can ensure the consistency of data types , It can allocate a large amount of memory at one time ?
answer : Yes ,C The method of allocating memory for language : Array
1.2. Definition of array : Is a method of allocating memory , The allocated memory can store multiple Data of the same type
1.3. Array of Advantages and disadvantages :
advantage : Can allocate a large amount of memory
shortcoming : The data type must be the same , Because some occasions require different data types , for example : fraction ( Floating point numbers ) And age ( Integers )
Must be allocated at one time , You can't use it at any time and distribute it at any time
1.4. Define the syntax format of the array
Element data type Array name [ length ( Also known as the number of array elements )] ={ Initialization list ( If there are more than one , Comma , Separate )};
for example :int a[5] = {1,2,3,4,5};
semantics : Continuous distribution 5 Memory space of elements , And the data type of each element is int, Each element accounts for 4 Byte memory space , So finally, the continuous distribution 20 Bytes of memory , And the value of each element is :1,2,3,4,5
similar :int a = 1, b = 2, c = 3, d = 4, e = 5;
1.5. Array characteristics
1. The memory allocated by the array is continuous
2. The array name is the first address of the entire array , It's equal to array number 0 The first address of an element
3. The length of an array is also called the number of array elements , Instead of the size of the entire array allocated memory
for example :int a[5]; The length of the array = Number of array elements =5, The memory size allocated is 20 byte
4. Array subscript is the number of elements in the array , from 0 Start
5. The elements in the array are accessed through the operator "[]" And subscripts to access
Format : Array elements = Array name [ Subscript ]
for example :int a[5] = {1,2,3,4,5};
The first 0 Elements :a[0] = 1
The first 1 Elements :a[1] = 2
The first 2 Elements :a[2] = 3
The first 3 Elements :a[3] = 4
The first 4 Elements :a[4] = 5
Print page 3 The value of the elements :printf("%d\n", a[3]);
Will be the first 3 Change the value of the elements to 250:a[3] = 250;
6. Pay attention to the cross-border access of the array ( The written examination questions are required )
Accessed memory that should not be accessed
for example :int a[5] = {1,2,3,4,5}; // Currently, the effective memory allocated by the operating system is 20 byte ,5 Elements
printf("%d\n", a[4]); // Print page 4 The value of the elements , legal
printf("%d\n", a[5]); // Printing does not exist 5 The value of the elements , Theoretically, it is beyond the limit , But the program didn't crash
// It is not clear what value to print , It doesn't crash because it just looks at the memory information
a[5] = 250; // A cross-border visit , Now the program crashes ,250 The number may have modified the core program of the operating system
1.6. Array definition form :
form 1:int a[5] = {1,2,3,4,5};
form 2:int a[5] = {1,2,3}; a[0]=1,a[1]=2,a[2]=3,a[3]=a[4]=0, If there is no initialization, give it to 0
form 3:int a[5] = {0}; Is full of 0
form 4:int a[5] = {}; Is full of 0
form 5:int a[5]; Only defined but not initialized , The results are random numbers
form 6:int a[] = {1,2,3,4,5}; The number of elements is 5 individual
form 7:int a[5] = {1,2,3,4,5,6,7,8}; gcc Ignore invalid elements directly ,6,7,8 Don't
Be careful : Wrong form :int a[]; // Compile not pass , Report errors
1.7. Formula for finding the number of array elements : Element number =sizeof( Array name )/sizeof( The first of an array of 0 Elements )
for example :int a[5] = {1,2,3,4,5};
Array first address =a=&a[0]
The amount of memory the array occupies =sizeof(a)=20
The first 0 Memory size occupied by elements =sizeof(a[0])=4
Element number =sizeof(a)/sizeof(a[0])=5 individual
1.8. Variable length array
Definition : The number of data elements is variable
for example : int a[5]; // Fixed length array , Fixed length ,gcc Continuous distribution in the future 20 Bytes of memory
int len;
int a[len]; // Variable length array , Uninitialized
scanf("%d", &len);
1.9. Multidimensional arrays
a) Definition of 2D array : An array consisting of multiple one-dimensional arrays , Each element of a two-dimensional array is an array , The essence of two-dimensional array is one-dimensional array , It's just a formal grouping of one-dimensional arrays
b) Define two-dimensional array syntax : Array element type 2D array name [ Two dimensional array length ][ One dimensional array length ] = { Initialize the table };
for example :int a[5][10];
semantics :
1.a The representative is a containing 5 Array of elements , Each of these elements is a containing 10 Array of elements , The data type of each element is int, therefore a Is the first address of the two-dimensional array
2.a[0] representative a Of the 0 Elements , And the first 0 Elements are also a one-dimensional array , therefore a[0] It's a one-dimensional array , And is the first address of a one-dimensional array
3.a[0][0] representative a Of the 0 The number of one-dimensional array elements 0 Elements , therefore a[0][0] Is an element in a two-dimensional array
4.a[i][j] representative a Of the i The number of one-dimensional array elements j Elements
c) Define the form of a two-dimensional array :
form 1:int a[3][4]; // Define only and do not initialize , random number
form 2:int a[3][4] = { {1,2,3,4},{4,3,2,1},{2,1,3,4}};
form 3:int a[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
form 4:int a[3][4] = {0};
form 5:int a[3][4] = { {1,2},{0}};
form 6:int a[][4] = {1,2,3,4,5,6,7,8}; Equivalent to int a[2][4]
form 7: int a[2][3] = { {1,2},{3,4},{5,6}}; // error , Transboundary
Be careful : The length of two-dimensional array can be omitted , But the length of one-dimensional array cannot be omitted
for example :
int a[3][4] = {
{1,2,3,4},{5,6,7,8},{9,10,11,12}};
printf("%d\n", a[2][3]); //12
printf("%d\n", a[0][3]); //4
printf("%d\n", a[1][2]); //7
a[2][2] = 250; //11->250
d) Two dimensional array induction :
for example :int a[2][3] = {0};
a Is the first address of a two-dimensional array =&a[0][0]
sizeof(a): Get the memory size occupied by the entire two-dimensional array
sizeof(a[0]): Get the second... In the two-dimensional array 0 Memory size occupied by one-dimensional array elements
sizeof(a[0][0]): Get the second... In the two-dimensional array 0 The... In a one-dimensional array 0 Memory size occupied by elements
sizeof(a)/sizeof(a[0]): Get the length of the two-dimensional array
sizeof(a[0])/sizeof(a[0][0]): Get the length of the one-dimensional array
2.0.goto sentence ( Go back where you came from )
2.1. effect : It can make CPU Jump to any statement to execute
application :linux The kernel operating system is heavily used goto sentence
2.2. grammar :
goto Tag name ; // The tag name specifies the place to jump
Be careful : The tag name is determined according to the identifier naming rules
2.3. Use form : Two kinds of
form 1:
Tag name :
sentence
...
goto Tag name ;
....
for example :
label:
printf("1\n");
goto label;
printf("2\n");
form 2:
goto Tag name ;
....
Tag name :
sentence ;
...
for example :
goto label;
printf("2\n");
label:
printf("1\n");
2.4.linux Kernel source code goto Examples of classic use of statements :
Start a program , Allocate three blocks of memory , As long as one memory allocation fails , Program end , And return the memory release that was successfully allocated to linux operating system
3.0. Empty statement
a) grammar : Just one ; sentence
for example :
int a = 1;
printf("a = %d\n", a);// Empty statement , It plays the role of delay
b) Application scenarios : Used to implement an empty loop
for example :
for(;;); // Empty loop , Give Way CPU Stop running down here
int i = 10000;
for(; i >= 0; i--); // Empty cycle 10000 Time , Every time CPU It takes time , This code acts as a delay , But it's not accurate .
c) Error paradigm :
int i;
for(i = 0; i < 5; i++) ; // A valid loop becomes an empty loop
printf("i = %d\n", i);
int i = 0;
while(i < 2); // A valid loop becomes an empty loop
....
Bear in mind : stay while/for The parentheses in the loop are misspelled ;, Unexpectedly, it becomes an empty loop
边栏推荐
- 通过proc接口调试内核代码
- WR | 西湖大学鞠峰组揭示微塑料污染对人工湿地菌群与脱氮功能的影响
- Copy mode DMA
- How to install MySQL
- Deeply cultivate 5g, and smart core continues to promote 5g applications
- [Jianzhi offer] 66 Build product array
- [729. My Schedule i]
- [729. My schedule I]
- 【729. 我的日程安排錶 I】
- Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
猜你喜欢
[brush questions] effective Sudoku
Enter a command with the keyboard
PHP talent recruitment system development source code recruitment website source code secondary development
Get ready for the pre-season card game MotoGP ignition champions!
The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
Jarvis OJ Flag
【jmeter】jmeter脚本高级写法:接口自动化脚本内全部为变量,参数(参数可jenkins配置),函数等实现完整业务流测试
Deeply cultivate 5g, and smart core continues to promote 5g applications
阈值同态加密在隐私计算中的应用:解读
Solution of vant tabbar blocking content
随机推荐
Learnopongl notes (II) - Lighting
Flet tutorial 12 stack overlapping to build a basic introduction to graphic and text mixing (tutorial includes source code)
Jarvis OJ 远程登录协议
【剑指 Offer】61. 扑克牌中的顺子
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
How to install MySQL
Can you help me see what the problem is? [ERROR] Could not execute SQL stateme
Jarvis OJ shell流量分析
干货!半监督预训练对话模型 SPACE
Twig数组合并的写法
【testlink】TestLink1.9.18常见问题解决方法
Games101 notes (I)
[first lecture on robot coordinate system]
网站页面禁止复制内容 JS代码
[61dctf]fm
Wsl2.0 installation
How does the outer disk futures platform distinguish formal security?
Yarn common commands
精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
Learnopongl notes (I)