A first introduction to pointers and structures in the third chapter of the C language

Objectives of this section

Understand the related concepts of memory and address, initially master the definition and use of pointers, understand the concept of structures, and master their basic use and access methods.

Getting to know the pointer

1. Memory and address

The memory is a particularly important memory on the computer, and the operation of the programs in the computer is carried out in the memory.
* So in order to use the memory efficiently, the memory is divided into small memory units, and the size of each memory unit is 1 byte.
* At the same time, in order to effectively access each unit of memory, the memory unit is numbered, and these numbers are called the address of the memory unit.

insert image description here

2. The address of the variable

Variables are created in memory (space is allocated in memory), and each memory unit has an address, so variables also have addresses.
Take out the variable address as follows:

#include <stdio.h>
int main()
{
    
    
 int num = 10;
 &num;//取出num的地址
    //注:这里num的4个字节,每个字节都有地址,取出的是第一个字节的地址(较小的地址)
 printf("%p\n", &num);//打印地址,%p是以地址的形式打印
 return 0;
}

insert image description here
insert image description here

3. Pointer variable

* Pointer variable: A variable used to store a pointer (a pointer is an address).

#include<stdio.h>
{
    
    
	int num = 10;
	int *p;//p为一个整形指针变量,int*是p的类型,其中*代表p是一个指针,int表示p指向的变量的类型是int的。
	p = &num;
	return 0;
}

4. Use of pointers

#include <stdio.h>
int main()
{
    
    
	int num = 10;
	int* p = &num;
	*p = 20;
	printf("%d\n", num);
	return 0;
}

insert image description here
insert image description here

int* p = &num : Define a pointer variable p, and assign the address of num to p. At this time, the address of num is stored in p, that is to say, p points to num;
*p = 20: * is a dereference operation symbol, *p is to dereference p, that is, to find num through the address of num stored in p, and modify the data 10 stored in the num space to 20;

5, the size of the pointer variable

#include <stdio.h>
int main()
{
    
    
	//%zu:用来打印无符号整型,与 %u 相似
	printf("%zu\n", sizeof(char*));   
	printf("%zu\n", sizeof(short*));
	printf("%zu\n", sizeof(int*));
	printf("%zu\n", sizeof(double*));
	return 0;
}

insert image description hereinsert image description here

* So: the pointer size is 4 bytes on 32-bit platforms and 8 bytes on 64-bit platforms. (essentially because the number of address lines under different platforms is different)

Getting to know the structure

1. What is a structure

A structure is an important data type in the C language, which consists of a set of different data called members (or fields, or elements), where each member can have a different type. Structures are often used to represent several types of data that are different but related.
The structure type is not defined by the system, but needs to be defined by the programmer. The C language provides the keyword struct to identify the defined structure type.

The keyword struct and the structure name are combined into a type identifier, whose status is the same as the usual type identifiers such as int, char, etc., and its use can be used to define structure variables just like the int type identifier identifies an integer variable. After the variable is defined, the variable can be used like other defined variables; members are also called member variables, which are several basic structure types contained in the structure, which must be enclosed in "{}" and must be marked with "{}". At the end of a semicolon, each member should indicate a specific data type.

2. Definition of structure

Let's take students as an example: Suppose a student includes five pieces of information: name, age, gender, and student ID. In order to describe a student as a whole, we can define a structure type.

struct Stu
{
    
    
    char name[20];//名字
    int age;      //年龄
    char sex[5];  //性别
    char id[15]; //学号
};

3. Use of structures

#include<stdio.h>
int main()
{
    
    
	//定义结构体变量并对其进行初始化
	struct Stu s = {
    
     "张三",18,"nan","220210101" };
	//两种结构体访问操作符
	// . 操作符
	printf("%s\t%d\t%s\t%s\n", s.name, s.age, s.sex, s.id);
	// -> 操作符
	struct Stu* ps = &s;  //定义结构体指针变量 ps 并将 s 的地址赋给 ps
	printf("%s\t%d\t%s\t%s\n", ps->name, ps->age, ps->sex, ps->id);
	return 0;
}

insert image description here

Note: The -> operator can only be used if there is already a struct pointer.

Guess you like

Origin blog.csdn.net/m0_62391199/article/details/124141089