Learning C++: A Review of the Basics

array

Find the length of a one-dimensional array:

double x[10];

sizeof(x) / sizeof(x[0]);

Find the number of rows in a two-dimensional array:

double x[5][5];
sizeof(x) / sizeof[x[0]];

sizeof(x) / sizeof(x[0][0]);

pointer

In a 32-bit system, the pointer type occupies 4 bytes of space, no matter what type of pointer is.
In a 64-bit system, the pointer type occupies 8 bytes of space, no matter what type of pointer

null pointer

Null pointer The pointer variable points to the space numbered 0 in the memory, which is used to initialize the pointer. Memory pointed to by a null pointer cannot be accessed

Wild pointer Pointer variables point to illegal memory space, try to avoid this situation.

//空指针定义
int * x = NULL;

pointer arithmetic

Pointer arithmetic can include ++, --, +, -, and can access array elements by incrementing the pointer, for example:

int arr[5] = {
    
    1, 2, 3, 4, 5};
int *p = arr;

for(int i = 0; i < 5; i++)
{
    
    
    cout << *p << endl;
    p++;
}

Pointer comparisons include >, <, ==.

int arr[5] = {
    
    1, 2, 3, 4, 5};

for(int *p = arr; p <= &arr[4]; p++)
{
    
    
    cout << *p << endl;
}

pointers vs arrays

The name of the array is a pointer constant, so the data in the array can be modified in the form of asterisks, but the address of the array cannot be modified. like:

int x[3] = {
    
    1, 2, 3};
*x = 0;
cout << *x << endl;
// x++; 报错

quote

References must be initialized, and once initialized cannot be changed.

Like pointers, references &are flexible in terms of symbol location when initialized. The following three methods are equivalent:

int x = 0;
int& y = x;
int & y = x;
int &y = x;
cout << y << endl;

References are commonly used as parameters or return values ​​of functions.

function

There are three ways to call a function: call by value, call by pointer, and call by reference.

When called by value, the formal parameter does not change the value of the actual parameter.

When called by pointer, the formal parameter will change the value of the actual parameter. Because the address is passed to the formal parameter, it is equivalent to the formal parameter directly operating the actual parameter.

When called by reference, the formal parameter will also change the value of the actual parameter.

constant

define constant

Use #define preprocessor.

#define LENGTH 10 

use the const keyword

const int LENGTH 10;

Note: It is good programming practice to define constants as capital letters.

Pointer constants and constant pointers

Pointer constant: pointer to const
constant pointer: const pointer

Whoever const is modified mainly depends on the asterisk, const is placed on the left side of the asterisk to modify the pointed object, const is placed on the right side of the asterisk to modify the pointer

As for the name, you can also use the asterisk as the dividing line, call it from the back to the front, and translate the asterisk into "if necessary" to. For example, const int * pnamed from the back to the front pointer to const int, it means a pointer to an integer constant, so the value pointed to by the pointer cannot be modified; if it is int * const pnamed const point to int, it means a constant pointer to an integer, the address of the pointer is immutable, but the pointed The value is variable.

According to the above rules, we can easily distinguish between correct and incorrect grammar. as follows:

Example 1:

int a = 100;
int b = 200;
const int * p = &a; //指向整型常量的指针,因此指针指向的值不可以改
//int const * p = &a; 二者等价

p = &b; //正确
// *p = 20; 报错,

Example 2:

int a = 100;
int b = 200;
int * const p = &a; //指向整型的指针常量,因此指针的指向不可以变。

//p = &b; //报错
*p = 20; //正确

Finally, a test:

1. const int p;	//整形常量p; const int
2. const int* p; //指向整形常量的指针p; pointer to const int
3. int const* p; //指向整型常量的指针p; pointer to const int
4. int * const p; //指向整形的常量指针p; const pointer to int
5. const int * const p; //指向整形常量的常量指针p; const pointer to const int
6. int const * const p; //指向整形常量的常量指针p; const pointer to const int

To sum up , we can find that the key to the difference between the two is the asterisk as the dividing line, depending on who is modified by const.

structure

Structure definition

struct Student
{
    
    
    string name;
    int age;
};

Student stu; //第一种初始化方式:C++
struct Student stu2; //第二种初始化方式:C或C++

In C++, the above-mentioned definition method can use the first method for initialization and function parameter passing; but in C language, the second method must be used. Therefore, in order to be compatible with the C language, the keyword can be omitted when the structure is initialized struct, and the following definition can be used:

typedef struct Student
{
    
    
    string name;
    int age;
} Student;

In this way, the subsequent Student is equivalent to the alias of the struct Student, and the Student can be used directly during initialization.

After defining the structure, the subsequent usage is basically the same as other variable types.

pointer to structure

A pointer to a struct ->accesses the internal members of the struct. example:

Student stu;
stu.name = "test";
stu.age = 17;

Student *p = &stu;

cout << p->name << endl << p->age << endl;

dynamic memory

Stack: All variables declared inside a function will occupy stack memory.
Heap: Unused memory in the program, which can be used to dynamically allocate memory while the program is running. The new operator is used to dynamically allocate heap memory, and the delete operator is used to release heap memory. like:

   //整型数据的动态内存分配
    int * p = NULL;
    p = new int;
    *p = 100;
    cout << *p << endl;
    
    delete p;

    //数组的动态内存分配
    int * p1 = NULL;
    p1 = new int[3];
    for(int i = 0; i < 3; i++)
    {
    
    
        p1[i] = i * i;
    }
    for(int i = 0; i < 3; i++)
    {
    
    
        cout << p[i] << endl;
    }

    delete[] p1;

references

おすすめ

転載: blog.csdn.net/xuyangcao123/article/details/124782405