Constructs a type of--constructed type. As for the definition, the author omitted, interested students can Baidu search https://www.baidu.com/. Today we're going to talk about the simplest constructed type in C--arrays (array).
Arrays are used to save and manipulate a set of data of the same type , forming a logical combination and accessing it through a uniform name, and you will find that the elements in the search or reference array are only values in square brackets, also known as index .
Each item of data in an array is called an element of an array , and each element is stored sequentially in memory, that is, the data is stored one after the other, not broken . Each element is accessed by the same array name and positioned using a positional ordinal, indicating that this is the first element in the array.
Watch out! In C, the array element is represented by an array name plus a square bracket (brackets), where the element is located . In addition to this, the subscript in the array brackets starts at 0 . The goal is to increase the speed of accessing individual elements, and if the starting subscript is 0, the compiler does not need to do more calculations to directly determine the internal offsets used to access subsequent elements, which is understood. below, let's take a closer look at arrays.
1, one-dimensional array (single dimensional array)
Declaration format: type an array group name [number of array elements]
Where the type name represents the data type of the elements stored in the array, such as int, float float, and so on. The array name can be taken by itself, but it is important to note that the name of the array is best based on what you have done to make some meaningful names, so it will not be easy to confuse and the program will be more standardized. Another notable point is that if you specify the number of elements in the array, only the integer data or an integer expression, such as [10], [5+5], or other form, can be in [].
Example: int names[10]; Parsing: int means that the elements in the array are integer data, the name is names, there are 10 elements in the array, which means that the index has a total of 10 elements from 0-9, ending with a semicolon; this is a sign of the end of a sentence, not careless to forget it. As for the amount of storage space that this array occupies, it depends on how much storage space your computer allocates to each data type, and here, if your computer allocates 4 bytes (bytes) of space to the int data, then the size of this array is 4*10 (bytes). That is, 40 bytes (one byte is 8 bits).
Initialization of an array element:
(1) Initialization of numbers
int number[3] = {1, 2, 3}; #注意, the values used for initialization are wrapped in curly braces.
Here, you can also omit the number of array elements in the declaration section, namely: int number[] = {1, 2, 3};
At this point, the compiler automatically determines the size of the array based on the number of elements initialized
We can also do this by defining it and then initializing it in the function, such as when you want to enter data through the user and then store it in an array.
(2) Initialization of character (character) and string (character string)
Char code[] = {' A ', ' B ', ' C '}; #字符的数据类型为char, so the type name here is char, of course, write int is also possible, because the INT data type takes up more storage space than char, the space allocated to the int type data is natural enough to hold the char type data, the number of elements above, can write not write , write the same number of initialization elements, or compile will error, character words need to "wrap up, like these letters, and some symbols are needed to deal with this
In addition to this, you can use strings to initialize directly, for example:
Char code[] = "ABC"; #但是要注意的是, a string constant enclosed in double quotation marks, at the end of the string is also implied as the end of the string flag-' ", in memory, this symbol is also a char data type, requires a character storage space
In fact, equivalent to char code[] = {' A ', ' B ', ' C ', ' n '};
In addition, there is an initialization method, which is defined first and then initialized in the function, which is mentioned earlier.
This note we can do here, in view of the author's limited level, is also learning, the text of what is missing or wrong place please prompt, I will make improvements, heartfelt thanks!
Array of C-language constructed types _01