• is an object in its own right, unlike reference.
  • also have two exceptios, the pointer and the object must match
  • Note that it is possible for a pointer to an object and a pointer one past the end of a different object to hold the same address

Null Pointers

  • ⭐️new standard introduce a literal nullptr.
  • the literal 0.
  • a preprocessor variable named NULL (in cstdlib header).

Attention

but can’t assign an int variable even if the value happens to be 0.

void* Pointers

The type void* is a special pointer type that can hold the address of any object.

  • we can compare it to another pointer.
  • we can pass it to or return it from a funtion.
  • we can assign it to another pointer.
  • we use a void * pointer to deal with memory as memory.

Pointers and const

Pointers to const

here const is a 🔗low-level const, so the pointer itself can change, but can’t change the underlain through the pointer.

int i = 0;
const int *p = i;

read from right to left: modifier indicates p is a pointer, and the base type says the pointer point to a const int.

const Pointers

here const is a 🔗top-level const, so the pointer itself cannot change, but can change the underlain through the pointer.

int i = 0;
int *const p = &i;
int const *p = &i;

again right from left.

Pointer Arithmetic

指针的加减法、比较等等,实际上是作为 iterators 时才有意义。而指针在数组中,是作为迭代器使用的,所以拥有迭代器的一切操作:

  • compare
  • addition
  • subtraction of two pointers