Pointers
Quick Notes
Section titled “Quick Notes”Pointer lưu địa chỉ của object.
Dùng *p để dereference và &x để lấy địa chỉ.
Trong C++ hiện đại, ưu tiên reference hoặc smart pointer khi phù hợp.
Syntax
Section titled “Syntax”int x = 5;int* p = &x;*p = 10;Example
Section titled “Example”#include <iostream>
int main(){ int values[] = {10, 20, 30}; int* p = values;
std::cout << *p << '\n'; // 10 std::cout << *(p + 1) << '\n'; // 20}Pointer vs Reference
Section titled “Pointer vs Reference”| Feature | Pointer | Reference |
|---|---|---|
| Null | Có thể nullptr | Không nên null |
| Rebind | Có thể trỏ sang object khác | Không rebind |
| Syntax | p->x, *p | Dùng như object |
Common Mistakes
Section titled “Common Mistakes”- Dereference
nullptr. - Trả pointer tới biến local.
- Pointer arithmetic ngoài vùng mảng hợp lệ.
Search Keywords
Section titled “Search Keywords”pointers con trỏ dereference địa chỉ pointer arithmetic nullptr pointer vs reference