First introduction to C language - first introduction to pointers and structures

1. Computer storage space - understand what an address is

When buying a computer, we usually hear how many gigabytes the computer has. The first part represents the memory , and the latter part represents its hard drive .

 

 The C language we learn is a high-level language, and its bottom layer is assembly language. The bottom layer is actually binary machine language.

Machine language is a language that a computer can directly read and understand.

When we download software, we may also encounter different versions of software, such as those suitable for 32-bit computers and those suitable for 64-bit computers. A 32-bit computer means that the size of an address is 32 bits, and a 64-bit computer means that the size of an address is 64 bits.

Below we take a 32-bit computer as an example:

If we want to access memory, we need the address to find the memory unit!

How is it? Isn’t it amazing? After understanding the address, we can start introducing pointers!

2. First understanding of pointers

2.1 Creation of pointer variables

We introduced the address earlier, now we give the pointer:

Memory number = address = pointer

Let’s introduce two more symbols that will be used below:

& : Get the address character

* : dereference character

What to note here is:

(1) The data type of the pointer variable ap should be the same as the data type of a 

(2) The pointer receives the address of the variable, so we need to take the address of the variable and put it on the right side of =, which will use the address operator &

(3) The * before ap indicates that ap is a pointer variable. If there is no *, how do you know that you represent a pointer? Rather than saying that your pointer is *ap. Therefore, I also suggest that you directly record the data type of the pointer as int*, double*...

(4) When we call the pointer, we need to add a * before the pointer variable name ap. This * represents the dereference character. For example, the pointer variable ap is a lock, and you need a key* to open the lock.

2.2 Size of pointer variables 

2.2.1 Review

Let's first recall the memory size occupied by different data types:

You can see that different data types occupy different memory spaces. The memory unit represented on the right is byte. 

2.2.2 Pointer size

Pointer variables are used to store addresses, so the size of the pointer depends on the size of the address.

In a 32-bit machine, the size of the pointer is 32 bits, which is 4 bytes.

In a 64-bit machine, the size of the pointer is 64 bits, which is 8 bytes.

tips: You can switch between 32-bit machines and 64-bit machines in VS  when we switch to 32-bit machines

 The size of each pointer will become 4byte.

 3. First understanding of structures 

After a general understanding of the C language, we can realize that the values ​​that can be created in the C language are too simple. When we have factors such as name, age, height, weight, etc. in real life, how do we use C language to represent these factors? Coming out? There are structures - tools that allow us to describe complex objects in C language.

3.1 Creation of structure

The creation of the structure should be placed above the main function.

struct stu{

};

struct is a keyword, telling the compiler that you want to create a structure; stu means you give the function body a name. This name is usually meaningful and easy to understand, and then you can create the information you want in the curly brackets.

Then you can define your structure in the main function.

 Struct stu is the structure we defined before. When actually using it, although stu is the name you give the structure, you still have to name each piece of information that uses the structure, so struct stu can be regarded as your The self-defined data type is the same as other data types int and double. The syntax uses data type + naming, that is, struct stu s1.

The syntax for inputting structure information is structure name + { }, and the information in the middle is separated by commas . Then you can enter the information of structure s1 in sequence according to the definition of the structure.

3.2 Printing of structure information

What should I do next if I want to print the structure?

 In the printf function, print each information in the structure in sequence. After the double quotes, enter the name you gave the structure variable and then add "." VS will automatically pop up  the keywords of the information in the structure.

That is: structure variables.structure members

 3.3 Pointer receiving function body

3.3.1 Grammar

In the usage of pointers, we mentioned that we can directly memorize pointers to name data structures*

In the naming of structures, we mentioned that the structures defined above the main function can be directly used as data types.

Combining it we can get the syntax: struct stu * p=&s1.

 3.3.2 Print structure

Different from non-pointers, the syntax of a structure that prints pointers becomes: structure pointer -> structure member

 Four, finished

Guess you like

Origin blog.csdn.net/m0_75186846/article/details/131270395