An array is an ordered collection of data elements of the same type. An array is represented by an array name, and the data in the array is uniquely identified by a specific subscript. The purpose of introducing an array is to use a continuous memory space to store multiple data of the same type to solve the storage problem of a batch of related data. Like ordinary variables, arrays must be defined before they can be used. Arrays play an important role in the C51 language, so it is necessary to have a deep understanding of arrays. The following is a detailed introduction to arrays.
(1) One-dimensional array
A one-dimensional array is the simplest array, used to store data of the same type. The data is stored linearly and continuously.
Use the following routine to illustrate array creation and data operations:
#include
/*
-------------------------------------------------- ---
This program is used to illustrate the creation of arrays and data operations.
-------------------------------------------------- ---
*/
unsigned char array[10]; //define an array with 10 elements
void main()
{
unsigned char i;
for(i=0;i<10;i++)
{
array[i]=i; //Use subscript to call the element in the array
}
/*
---------------------------------------
array |9|8|7|6|5|4|3|2|1|0| [9]~[0]
---------------------------------------
*/
while(1);
}
The array name is used to identify the array. In fact, it is the first address of the array, that is, a pointer. However, the address it represents is fixed and cannot be changed. As mentioned in the previous chapters, array[2] is equivalent to *(array+2), but array++ cannot be used because array is a constant.
The array in the above program is created statically. The following example illustrates the dynamic creation of the array.
#include
#include
/*
-------------------------------------------------- ---
This program is used to illustrate the dynamic creation of an array.
-------------------------------------------------- ---
*/
unsigned char *parray;
void main()
{
unsigned char i;
parray=(unsigned char *)malloc(10); //dynamically create an array
for(i=0;i<10;i++)
{
parray[i]=i; //Assign values to the array
}
free(parray); //release the array
while(1);
}
A string is an important special case of an array. The data of each unit is of character type (char), and the last unit is '' (0x00), which is used to indicate the end of the string. The C51 function library provides functions specifically for processing strings, as illustrated by the following routine:
#include
#include
/*
-------------------------------------------------- ---
This program is used to illustrate the string
-------------------------------------------------- ---
*/
char s[]={'y','a','h','o','o',''};
//Define a string and initialize it, ending with ''
void main()
{
char s_temp[10];
strcpy(s_temp,s); //strcpy is located in the string.h header file and implements character copying
//s is a constant, not s++
strcpy(s_temp,"yahoo");//Equivalent to the above statement
while(1);
}[page]
The following are some flexible uses of strings, hoping to help readers gain a deeper understanding of strings:
#include
#include
/*
-------------------------------------------------- ---
This program is used to illustrate the flexible use of strings
-------------------------------------------------- ---
*/
/*
-------------------------------------------------- ---
This function extracts the nth substring from string s, where substrings are separated by ','
Returns a pointer to the substring
-------------------------------------------------- ---
*/
char *get_sub_string(char *s,unsigned char n)
{
int i;int d=0;int fore=0;
int len=strlen(s);
for(i=0;i
{
if(s[i]==',')
{
s[i]='';
d++;
if(d==n)
{
return s+fore;
}
else
{
fore=i+1;
}
}
}
return NULL;
}
void main()
{
unsigned char c;
char string[20];
c="yahoo"[2]; //c='h'
/*As mentioned earlier, a string is represented by the first address of the string.
The string "yahoo" is actually its first address, so it can be done like this
Take a character from it: "yahoo"[2]*/
strcpy(string,"123,234,345,456");
strcpy(string,get_sub_string(string,2));
while(1);
}
(2) Two-dimensional array
An array whose elements can be determined by two subscripts is called a two-dimensional array. The general form of its definition is:
Type specifier array name [constant expression 1] [constant expression 2]
For example: int array[6][4];
A two-dimensional array array is defined with 6 rows and 4 columns, totaling 24 elements.
The constant expression 1 and constant expression 2 in the two square brackets specify the number of rows and columns of the array, thereby determining the number of elements in the array. The row subscript starts from 0 and the maximum is 5, with a total of 6 rows; the column subscript also starts from 0 and the maximum is 3, with a total of 4 columns. There are a total of 6X4=24 elements in the array, which are expressed as follows:
array[0][0] |
array[0][1] |
array[0][2] |
array[0][3] |
array[1][0] |
array[1][1] |
array[1][2] |
array[1][3] |
array[2][0] |
array[2][1] |
array[2][2] |
array[2][3] |
array[3][0] |
array[3][1] |
array[3][2] |
array[3][3] |
array[4][0] |
array[4][1] |
array[4][2] |
array[4][3] |
array[5][0] |
array[5][1] |
array[5][2] |
array[5][3] |
In actual use, the above two-dimensional array can be regarded as a matrix with 6 rows and 4 columns, which is a flat two-dimensional structure. So how does the compiler use one-dimensional storage space to allocate continuous storage units to such a two-dimensional structure? C51 uses the row storage method, that is, the 0th row elements are stored in the memory first, and then the 1st row, 2nd row, ... elements are stored. In each row, the 0th column is stored first, followed by the 1st column, 2nd column, ... elements.
#include
#include
/*
-------------------------------------------------- ---
This program is used to illustrate the use of two-dimensional arrays
-------------------------------------------------- ---
*/
void main()
{
unsigned char arrays[3][3]={{1,2,3},{2,3,4},{3,4,5}};
//Define a 3-row 3-column 2D array. The rest is stored in memory in a 1D manner.
//You can know this by using the following method
unsigned char test;
test=((unsigned char *)arrays)[6];//test=3;
//Convert the first address of the two-dimensional data into a one-dimensional array and access it as a one-dimensional array
while(1);
}
In addition to one-dimensional arrays and two-dimensional arrays, you can actually define arrays of any dimension. Multidimensional arrays are used to represent quantities that can only be determined by multiple subscripts.
For example: int arrays[3][3][3]
Represents array arrays as a three-dimensional array, corresponding to the three-dimensional storage model.
In fact, the memory in the microcontroller is one-dimensional, that is, all data is stored sequentially, so no matter how many dimensions the array has, the compiler abstracts the array to the actual one-dimensional array mapping stored in the microcontroller.
#include
/*
-------------------------------------------------- ---
This program is used to illustrate the three-dimensional array
-------------------------------------------------- ---
*/
void main()
{
unsigned char test;
unsigned char arrays[2][2][2]={{{1,2},{2,3}},{{3,4},{4,5}}};
test=arrays[1][1][0]; //test=4
test=((unsigned char *)arrays)[7]; //test=5
while(1);
}[page]
(3) Structure array
Multiple structure variables can also form a structure array, and the definition method is exactly the same as that of defining a structure variable.
As an example:
#include
/*
-------------------------------------------------- ---
This program is used to illustrate the structure array
-------------------------------------------------- ---
*/
typedef struct
{
int a,b,c,d;
} Stru;
void main()
{
Stru stru[10]; //define structure array
unsigned char i=0;
for(;i<10;i++)
{
stru[i].a=i;
stru[i].b=i;
stru[i].c=i;
stru[i].d=i;
}
while(1);
}
Previous article:Design of household smoke alarm based on MSP43O microcontroller
Next article:MN101EF32D single chip microcomputer realizes oscillometric blood pressure measurement
Recommended ReadingLatest update time:2024-11-16 15:30
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- How to import pictures in AD19 version and run script software crash
- JD642 SDRAM bus matching pre-simulation and pcb download
- [STM32WB55 Review] 6# Use of STM32WB development board UART
- [New Year's Taste Competition] + New Year's Eve dinner + New Year's 7 days of fun, I wish you all a prosperous New Year, smooth sailing and a good start.
- Sell TI POS Kit Chip
- cc2530 zigbee-zha modification supports serial port 1 position 1 P0_4 P0_5
- EEWORLD University ---- Learn myRIO with me
- Classic MATLAB simulation model for parameter identification of three-phase asynchronous motor
- 900 yuan, looking to buy a DE1-SOC, no accessories are required [
- EEWORLD University Hall----Introduction to FPGA Peripheral Circuits (Intel Official Tutorial)