滴水穿石 - ************

1 minute read

Pointers, again… How many pointers you can have?
C89: 12

Week31~35 (07/29~08/31)

網路文章

What is the difference between const int*, const int * const, and int const *?
Read it backwards (as driven by Clockwise/Spiral Rule):
int* - pointer to int
int const * - pointer to const int
int * const - const pointer to int
int const * const - const pointer to const int

Now the first const can be on either side of the type so:
const int * == int const *
const int * const == int const * const

If you want to go really crazy you can do things like this:
int ** - pointer to pointer to int
int ** const - a const pointer to a pointer to an int
int * const * - a pointer to a const pointer to an int
int const ** - a pointer to a pointer to a const int
int * const * const - a const pointer to a const pointer to an int

const X* p means “p points to an X that is const”: the X object can’t be changed via p.
X* const p means “p is a const pointer to an X that is non-const”: you can’t change the pointer p itself, but you can change the X object via p.
const X* const p means “p is a const pointer to an X that is const”: you can’t change the pointer p itself, nor can you change the X object via p.

網路資源

Stanford CS143: Compilers

Leave a Comment