当前位置:网站首页>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
边栏推荐
- Jarvis OJ 远程登录协议
- 项目引入jar从私服Nexus 拉去遇到的一个问题
- C how TCP restricts the access traffic of a single client
- Raspberry pie 4B installation pytorch1.11
- 中国广电正式推出5G服务,中国移动赶紧推出免费服务挽留用户
- Detailed explanation of use scenarios and functions of polar coordinate sector diagram
- Facing new challenges and becoming a better self -- attacking technology er
- Allusions of King Xuan of Qi Dynasty
- [Jianzhi offer] 61 Shunzi in playing cards
- Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
猜你喜欢
The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
美国芯片傲不起来了,中国芯片成功在新兴领域夺得第一名
Games101 notes (I)
飞桨EasyDL实操范例:工业零件划痕自动识别
How to install MySQL
【729. 我的日程安排錶 I】
精准防疫有“利器”| 芯讯通助力数字哨兵护航复市
ECU简介
Global Data Center released DC brain system, enabling intelligent operation and management through science and technology
If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
随机推荐
Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
Benji Banas membership pass holders' second quarter reward activities update list
Games101 notes (II)
[wechat applet] read the life cycle and route jump of the applet
Is it safe to open an account for digging wealth stocks? How is it safe to open a stock account?
7.Scala类
【性能测试】全链路压测
The first EMQ in China joined Amazon cloud technology's "startup acceleration - global partner network program"
C language to get program running time
It is forbidden to copy content JS code on the website page
采用药丸屏的iPhone14或引发中国消费者的热烈抢购
Is it safe to open a securities account by mobile phone? Detailed steps of how to buy stocks
Fleet tutorial 09 basic introduction to navigationrail (tutorial includes source code)
How was the middle table destroyed?
【机器人坐标系第一讲】
外盘期货平台如何辨别正规安全?
网上办理期货开户安全吗?网上会不会骗子比较多?感觉不太靠谱?
File operation --i/o
Jarvis OJ shell流量分析
Clear restore the scene 31 years ago, volcanic engine ultra clear repair beyond classic concert