Usage analysis of Pointers and const qualifiers

  • 2020-04-01 23:28:49
  • OfStack

There are several common cases where const qualifiers and Pointers are combined.

(1) pointer to a constant

Const int * a; Int const * a;

The two writing methods are the same, a is a pointer to const type, a points to the memory unit cannot be overwritten, so (*a)++ is not allowed, but a can be overwritten, so 0 a++1 is allowed.

What the pointer points to doesn't change, but the pointer itself can.

(2) a pointer

Const int * a;

a is a pointer to int type const, *a can be overwritten, but a is not allowed to be overwritten.

The pointer itself cannot be changed, but what the pointer points to can be changed.

(3) a constant pointer to a constant

Int const * const a;

a is a pointer to const type const, so *a and a are not allowed to be overwritten.

The pointer itself and what it points to are not allowed to change.

The above three situations if you learn c++, there should be contact!


Related articles: