Skip to content

Pointers

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.

int x = 5;
int* p = &x;
*p = 10;
#include <iostream>
int main()
{
int values[] = {10, 20, 30};
int* p = values;
std::cout << *p << '\n'; // 10
std::cout << *(p + 1) << '\n'; // 20
}
FeaturePointerReference
NullCó thể nullptrKhông nên null
RebindCó thể trỏ sang object khácKhông rebind
Syntaxp->x, *pDùng như object
  • Dereference nullptr.
  • Trả pointer tới biến local.
  • Pointer arithmetic ngoài vùng mảng hợp lệ.

pointers con trỏ dereference địa chỉ pointer arithmetic nullptr pointer vs reference