The text below is part of my handout and part of my tutorial. It is very basic, but it is very distinctive. If you are interested in reading it well, you can reply to it if you do not understand or do not know it, it is possible that we will talk about it in the live classroom next week. Please pay attention to it.
Chapter 2 storage in the program
Chapter 1 describes the basic working principles of computers, binary data, and how to digitize data. Next we will start to enter the programming field and how to use the memory space, how to store and read large volumes of data in a organized manner in the memory.
1. Data Space
The space we use to store data is called the data space. What corresponds to the data space is the address space (pointer). During the operation, we need to store a large amount of data, (In a ball game, we need to store balls, colors, and balls. Based on the data, we can perform ball cutting and moving ,) the most common is the storage of numbers and characters. In order to store data, we usually need to open up space or apply for space. First, we need to learn how to open the space, that is, the variable declaration statement:
1). Open Space
Int A; open up a space of the int type. A is the name of the space.
Char C; open up a char Type Space C
This statement is very simple. It consists of two parts: the first part indicates the type of the space to be opened. In 32-bit computers, the int type is 4 bytes, And the char type is 1 byte, the last part is the name of the space. This name is for you. It is also called an identifier. The name is easy to understand and requires many names when writing a program, the name is also a bit knowledgeable. Try to use English words. At first glance, you can understand the meaning. Try not to use Pinyin, or letters with serial numbers. You cannot start with a number. You can only use letters and underscores (_) it can start with, and contain numbers, letters, and underscores (_) in the middle (_). In the current textbook, this statement is called a variable declaration.
In addition to int and char, there are some other types, which are commonly used as follows:
Type length storage content
Char 1, 0-255
Int 4 signed integer
Long 4 signed integer
Unsigned 4 unsigned integer
Short 2 signed integer
Float 4 signed decimal places
Double 8 signed integer
Note: The C language requires that ";" be added to the end of each sentence.
2) space assignment
Please refer to the code
Int;
Char B;
A = 258;
B = 'B ';
The equal sign indicates that the value on the right of the equal sign is stored in the space on the left of the equal sign. If the value on the left is not a ready space, it must be wrong.
Note:
1.258 requires two bytes to store 102. The content in the previous chapter is low, so 02 is front and 1 is back. The number of digits not used in the front must be 0.
2. Single quotes indicate the ASC code value corresponding to the letter or number. Each pair of single quotes can only contain one character;
We usually store the content in one space to another.
For example, B = A; store the content of A in B.
However, when the two space types are different, some problems occur:
For example, char M = 30;
Int B = 258;
B = m;
The space of B is larger than that of M. It is more than enough to put M content into B, so there will be no problems. The extra space must be cleared or 1, (Why 1 ), make sure that the put value is equal to the retrieved value.
But M = B; what will happen? B's space is larger than m, so it cannot be put down. In this case, the computer's default processing is to put the bytes of B, but the compiler will ask us to write and convert it as follows:
M = (char) B; then the compiler will know that this is what we did on purpose, rather than accidentally making a mistake.
Based on the above reasons, all values in a large space must be displayed and converted into a small space. Otherwise, they are not needed.
2. Array
When we need to open multiple spaces of the same type, we can use an array as a simple method.
For example, int A [10]; indicates that 10 int-type spaces are enabled.
These 10 spaces are respectively a [0], a [1]…. A [9] is used in exactly the same way as a separate open space. Save the values of the 10 spaces to 0. The Code is as follows:
A [0] = 0;
A [1] = 0;
A [2] = 0;
A [3] = 0;
A [4] = 0;
A [5] = 0;
A [6] = 0;
A [7] = 0;
A [8] = 0;
A [9] = 0;
There is also a simple Syntax:
Int A [] = {0, 0, 0, 0, 0, 0, 0}; this sentence includes int A [10], but first writes int A [10], writing a [] = {0, 0, 0, 0, 0, 0, 0, 0} is incorrect.
Generally, you must specify the number when opening an array space. However, when opening a space, there is a value assignment statement directly. The value assignment statement can indicate the number. In this case, you do not need to specify the number.
It must be noted that the space opened by the array must be continuous, and the continuity cannot be ensured.
When it comes to arrays, we need to mention strings and look at the Code:
Char STR [] = "This ";
The content enclosed in double quotation marks is called a string. This string is stored in the array 'str' and occupies 5 bytes in total, except for 'T', 'h', 'I ', '/0' is added as the end character. Note that it is not '0 ',
Char str1 [] = {'T', 'h', 'I','s '};
Str1 is also an array. It occupies only four bytes and does not end with any ending character. Therefore, it cannot be accessed as a string array.
When we access a string array, we only need the first address, no length or end address. When we access the character '/0, it is considered that the string is over.
2. Address Space (also called pointer space)
2.1 Definitions and usage of address space (pointer)
Unlike the space described above, there is a space that is used not to store data but to store addresses, which is called pointer space. why are pointers not easy to understand? It is mainly because only the pointer can point to a space. Ignoring the pointer itself is also a space, only the address can be stored. Let's take a look at the usage:
Int A; open a common space of int type
Int * m; opens a space for storing a's address. m
M = & A; store the address of space a in address space M; & symbol indicates the address for retrieving space, which can be used before any space. for example, & M indicates the address of M space.
M stores the address of a and you can use M to access space.
* M = 26; * indicates the space pointed to by the pointer. It must be used before the pointer space. Here, it is equivalent to storing a number 26 in space a through M. * It cannot be used before the data space. if a pointer space is not assigned a value (also called initialization), The * operator cannot be used.
2.2 type of address space (pointer)
We note that the type is also described when the address space is opened, but it is completely different from the type of the common space. The type of the common space mainly specifies the size of the space, the size of the address space is the same. For any type of space, their address length is the same. The type of the pointer space has two purposes:
First, when the * symbol is used, the size of the access space is determined:
Int;
Int * m
M = &;
* M = 26;
At this time, M is a pointer of the int type. m points to the first byte of space a. When * m is used for access, both the read and modification access the space of 4 bytes.
Second, when performing pointer operations:
The pointer can be operated, but only addition and subtraction can be performed, indicating a relative backward offset or a forward offset. The content of the two pointer spaces cannot be added and the addition does not have practical significance, but can be subtracted, subtraction means the difference between the two pointer spaces.
Int A [10];
Int * m;
M = & A [0];
M = m + 1; can also be written as M ++;
Here, m + 1 does not mean that the value in M is added with 1, but 1 * (the size of the int space). Here, int is the type of M, after M = m + 1, m stores the address of a [1] instead of the second byte in a [0. when accessing arrays, we can usually use pointer operations to access different elements.
The above two points describe the usage of the pointer type and also explain the meaning of the pointer operation. If a pointer does not specify a type, it cannot be computed or use the * operator? Yes. Void * indicates no type pointer.
2.3 pointer type conversion.
The so-called pointer type conversion is to put the pointer of the type into the pointer space of the B type. In principle, the size of all pointer spaces is the same and there is no size mismatch problem, however, due to the special usage of pointer types, to prevent errors, the compiler requires forced conversion when assigning values to different types of pointer spaces.
For example:
Char * m;
Int * N;
M = (char *) N;
N = (int *) m;
2.4 pointers and arrays, multiple pointers
This is something that is easy to confuse. First look at the Code:
Int A [10]; // enables 10 int spaces, and uses a [0] to a [9] to correspond to 10 spaces respectively.
But if I take the ticket out, can I do it? We didn't talk about it just now. I can talk about it after I learned the pointer. If I only write a, I don't need the '[]' next to it. it indicates the address of a [0], that is, & A [0]. if you open a pointer space int * P; you can write P = & A [0]; you can also write P = A; the two statements mean exactly the same.
A pointer is also a space. When you need to open multiple pointers at the same time, you can use an array called a pointer array. The Code is as follows:
Char * P [10]; // the pointer space of 10 Char Types is opened at a glance, and the pointer space of 10 Char Types is corresponding to the 10 pointer spaces respectively from P [0] to P [9.
At this time, write P separately without the '[]'. The meaning is the same as described above: & P [0]. If you want to save & P [0, you need to open a double pointer to read the code.
Char ** m; M = & P [0] or M = P; When a * is used, * m and P [0] have the same meaning and are still a pointer, when ** m is used, it represents a char space, which is called multiple pointers. To put it bluntly, it is the pointer of the pointer.
3. struct Space
Struct space is actually nothing special, but the type of the space for opening struct is quite special. Let's take a look at a piece of code.
Struct Unit
{
Int X;
Int y;
Char haveball;
Char ballcolor;
};
This Code defines a struct, where struct is the keyword. First, explain the keyword. the keyword is an English word with a fixed meaning in the C language. We cannot use the keyword to name it when writing a program, the data type we mentioned above is also a keyword.
Struct defines a new struct. The Unit is our own name, followed by a pair of braces, which contains the code for opening a space, indicating what kind of space the new type is composed of, and it does not really open a space, end. A new struct is a new type. The code above only defines a new type. We use a new type to open the space, that is, the struct space. The Code is as follows;
Unit;
The new space consists of four spaces: int, Int, Char, and char. When we use this space, it is impossible to directly "A = ...;" Assign values respectively. The Code is as follows:
A. x = 1;
A. Y = 1;
A. haveball = 1;
A. ballcolor = 2;
The struct space can also be used as a pointer.
Unit * pA;
Pa = &;
In the same way, PA can be used to access the content in space:
(* PA). x = 2;
But there is a simple way to write
Pa-> X = 2; // the meaning of the above sentence is exactly the same.