const int = int const?

Viewed 87799

For example, is

int const x = 3;

valid code?

If so, does it mean the same as

const int x = 3;

?

7 Answers

I found that if always write const in right, it will becoming easier to read a complex pointer type, for fun.

For example:

// Just append const or pointer to
int x; // data
int const cx; // const data
int *px; // pointer to data
int const *pcx; //pointer to const data
int * const cpx; // const pointer to data
int const * const * * const cppcpcx; // const pointer to (pointer to (const pointer to const data))

I have an idea how to interpret (non)const pointers and data. Split it with *.

Data type Star sign Pointer type Combine Data Pointer
int * p int *p non-const non-const
const int * p const int *p const non-const
int * const p int *const p non-const const
const int * const p const int *const p const const
Related