1 Compile the software
Visual Studio
2 basic grammar
2.1 pointer
2.1.1 The use of pointers
//定义一个指针
int a = 10;
int * p;
//让指针记录变量a的地址
p = &a;
cout << "a的地址:" << &a << endl;
cout << "p:" << p << endl;
cout << "p:" << *p << endl;
//使用指针
//可以通过解引用的方式来找到指针
*p = 1000;
cout << "a的地址:" << a << endl;
cout << "p:" << *p << endl;
2.1.2 Pointer memory
Occupies 4 bytes on a 32-bit OS and 8 bytes on a 64-bit OS
After changing to 64 bits, the memory of the pointer is 8 bits
2.1.3 Null pointer
The pointer variable points to the space numbered 0 in the memory to initialize the pointer variable, and the memory pointed to by the null pointer cannot be accessed.
//定义一个指针 指针变量指向内存地址为0的空间
int * p = NULL;
//访问空指针报错 内存编号为0-255为系统占用 不允许用户访问
*p = 100;
cout << "p:" << p << endl;
cout << "*p" << *p << endl;
2.1.4 Field pointers
Pointer variable points to illegal memory space
Neither the null pointer nor the wild pointer is the memory space we applied for, and neither can be accessed.
2.1.5 const modified pointer
const modified pointer: constant pointer
The pointing of the pointer can be modified The value pointed to by the pointer cannot be modified
const modified constant: pointer constant
The pointing of the pointer cannot be modified but the value pointed to by the pointer can be modified
const modifies both pointers and constants
Neither the pointer nor the value pointed to by the pointer can be modified
2.1.6 Pointers and arrays
2.1.7 Pointers and functions
Pass by value : does not change the actual parameter
Address passing : the value of the actual parameter will be modified
2.2 Structure
2.2.1 Definition and creation of structure
User-defined data types, allowing users to store different data types
Several ways to create and use, struct can be omitted when creating a structure object, but cannot be omitted when defining
2.2.2 Structure array
Put the custom structure into the array.
2.2.3 Structure Pointer
Regarding object calling members, use "." or "->", depending on whether the member is on the heap or on the stack, use -> in the heap, and use . in the stack.
2.2.4 Structure Nested Structure
It should be noted here that if you want to use the Student structure in Teacher, you must first create the Student structure
2.2.5 Structure as function parameter
It can be seen that the value in the main function has also changed in the address transfer, and the value in the Main function has not changed in the value transfer
2.2.6 Const usage scenarios in structures
Changing the formal parameters in the function to pointers can reduce the memory space and will not make new copies. But in order to prevent the data in the actual parameter from being modified, adding const before the data can solve the problem, so that the actual parameter will not be modified.
2.3 References
Role: alias a variable data type & alias = original name
The reference and the original name point to the same memory.
2.3.1引用的注意事项
1 引用必须初始化
2 初始化后不能被改变
2.3.2引用作函数参数
函数传参时,可以利用引用让形参修饰实参,简化指针修改实参
在值传递中,不能改变实参的值,地址传递可以改变实参,引用传递也可以。
2.3.3引用作函数的返回值
引用的本质是一个指针常量,指针的指向不可以修改,但是指针指向的值可以修改。
不要返回局部变量的引用
函数的返回值是一个引用,那么函数的调用可以作为一个左值。
2.3.4常量引用
用来修饰形参防止误操作
引用必须指向一个内存空间,栈区或者堆区,不能是常量区中的。
加一个const常量就可以解决问题,相当于先创建一个temp=10,然后将引用指向temp
int a = 10;
const int & ref = 10; //int temp=10; const int & ref = temp;