Share 02x c++

thucdaochun

New member
#02X, C ++, C ++ Hướng dẫn, Con trỏ, Tài liệu tham khảo ## 02X C ++ Hướng dẫn: Con trỏ và tài liệu tham khảo

Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về các gợi ý và tài liệu tham khảo trong C ++.Con trỏ và tài liệu tham khảo là cả hai cách để tham khảo một vị trí bộ nhớ, nhưng chúng có những cách sử dụng khác nhau.

### Con trỏ

Một con trỏ là một biến lưu trữ địa chỉ của một biến khác.Khi bạn khai báo một con trỏ, bạn phải chỉ định loại dữ liệu của biến mà nó sẽ chỉ ra.Ví dụ: mã sau tuyên bố một con trỏ tới số nguyên:

`` `C ++
int* con trỏ_to_integer;
`` `

Biểu tượng `*` trong khai báo chỉ ra rằng `con trỏ_to_integer` là một con trỏ.

Để gán giá trị cho một con trỏ, bạn có thể sử dụng toán tử `->`.Toán tử `->` được gọi là toán tử mũi tên và nó được sử dụng để truy cập các thành viên của một cấu trúc hoặc lớp thông qua một con trỏ.Ví dụ: mã sau gán giá trị 10 cho số nguyên được trỏ đến bởi `polia_to_integer`:

`` `C ++
*con trỏ_to_integer = 10;
`` `

Bạn cũng có thể sử dụng con trỏ để chuyển các đối số cho các chức năng.Khi bạn chuyển một con trỏ đến một hàm, hàm có thể sửa đổi giá trị của biến mà con trỏ trỏ đến.Ví dụ: mã sau đây gọi hàm `change_value ()`, sửa đổi giá trị của số nguyên được trỏ đến bởi `con trỏ_to_integer`:

`` `C ++
void Change_value (int* con trỏ) {
*Con trỏ = 20;
}

int main () {
int value = 10;
int* con trỏ_to_value = & value;

thay đổi_value (con trỏ_to_value);

cout << value << endl;// In 20

trả lại 0;
}
`` `

### Người giới thiệu

Một tham chiếu là một cách để tham khảo một biến mà không tạo một bản sao mới.Khi bạn khai báo tham chiếu, bạn phải chỉ định loại dữ liệu của biến mà nó sẽ đề cập đến.Ví dụ: mã sau tuyên bố tham chiếu đến số nguyên:

`` `C ++
int & tham chiếu_to_integer;
`` `

Biểu tượng ampersand (&) trong khai báo chỉ ra rằng `tham chiếu_to_integer` là một tham chiếu.

Để gán giá trị cho tham chiếu, bạn chỉ cần sử dụng toán tử gán (=).Ví dụ: mã sau gán giá trị 10 cho số nguyên được đề cập bởi `tham chiếu_to_integer`:

`` `C ++
tham chiếu_to_integer = 10;
`` `

Bạn cũng có thể sử dụng các tài liệu tham khảo để truyền đối số cho các chức năng.Khi bạn chuyển tham chiếu đến một hàm, hàm có thể sửa đổi giá trị của biến mà tham chiếu đề cập đến.Ví dụ: mã sau gọi hàm `Change_value ()`, sửa đổi giá trị của số nguyên được đề cập bởi `tham chiếu_to_integer`:

`` `C ++
void Change_value (int & tham chiếu) {
tham chiếu = 20;
}

int main () {
int value = 10;
int & tham chiếu_to_value = value;

thay đổi_value (tham chiếu_to_value);

cout << value << endl;// In 20

trả lại 0;
}
`` `

### Sự khác biệt giữa con trỏ và tài liệu tham khảo

Sự khác biệt chính giữa các con trỏ và tài liệu tham khảo là các con trỏ có thể được gán lại để chỉ ra một biến khác nhau, trong khi các tài liệu tham khảo không thể.Ví dụ: mã sau là hợp lệ:

`` `C ++
int* con trỏ_to_integer = & value;
con trỏ_to_integer = & khác_value;
`` `

Tuy nhiên, mã sau không hợp lệ:

`` `C ++
int & tham chiếu_to_integer = value;
tham chiếu_to_integer = other_value;
`` `

Một sự khác biệt khác giữa con trỏ và tài liệu tham khảo là con trỏ có thể vô hiệu, trong khi các tài liệu tham khảo không thể.Một con trỏ null là một con trỏ không chỉ vào bất kỳ biến nào.Ví dụ: mã sau là hợp lệ:

`` `C ++
int* con trỏ_to_
=======================================
#02X, C++, C++ tutorial, pointers, references ## 02x C++ Tutorial: Pointers and References

In this tutorial, we will learn about pointers and references in C++. Pointers and references are both ways to refer to a memory location, but they have different uses.

### Pointers

A pointer is a variable that stores the address of another variable. When you declare a pointer, you must specify the data type of the variable that it will point to. For example, the following code declares a pointer to an integer:

```c++
int* pointer_to_integer;
```

The `*` symbol in the declaration indicates that `pointer_to_integer` is a pointer.

To assign a value to a pointer, you can use the `->` operator. The `->` operator is called the arrow operator, and it is used to access the members of a structure or class through a pointer. For example, the following code assigns the value 10 to the integer pointed to by `pointer_to_integer`:

```c++
*pointer_to_integer = 10;
```

You can also use pointers to pass arguments to functions. When you pass a pointer to a function, the function can modify the value of the variable that the pointer points to. For example, the following code calls the `change_value()` function, which modifies the value of the integer pointed to by `pointer_to_integer`:

```c++
void change_value(int* pointer) {
*pointer = 20;
}

int main() {
int value = 10;
int* pointer_to_value = &value;

change_value(pointer_to_value);

cout << value << endl; // Prints 20

return 0;
}
```

### References

A reference is a way to refer to a variable without creating a new copy. When you declare a reference, you must specify the data type of the variable that it will refer to. For example, the following code declares a reference to an integer:

```c++
int& reference_to_integer;
```

The ampersand (&) symbol in the declaration indicates that `reference_to_integer` is a reference.

To assign a value to a reference, you can simply use the assignment operator (=). For example, the following code assigns the value 10 to the integer referred to by `reference_to_integer`:

```c++
reference_to_integer = 10;
```

You can also use references to pass arguments to functions. When you pass a reference to a function, the function can modify the value of the variable that the reference refers to. For example, the following code calls the `change_value()` function, which modifies the value of the integer referred to by `reference_to_integer`:

```c++
void change_value(int& reference) {
reference = 20;
}

int main() {
int value = 10;
int& reference_to_value = value;

change_value(reference_to_value);

cout << value << endl; // Prints 20

return 0;
}
```

### Differences between pointers and references

The main difference between pointers and references is that pointers can be reassigned to point to a different variable, while references cannot. For example, the following code is valid:

```c++
int* pointer_to_integer = &value;
pointer_to_integer = &another_value;
```

However, the following code is invalid:

```c++
int& reference_to_integer = value;
reference_to_integer = another_value;
```

Another difference between pointers and references is that pointers can be NULL, while references cannot. A NULL pointer is a pointer that does not point to any variable. For example, the following code is valid:

```c++
int* pointer_to_
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top