-1

I have learned (correct me if I am wrong) much about the keyword const and I learned that it is very easy if you read it from the right to the left like this:

int a = 0;                 // `a` is an integer. Its value is initialized with zero
const int b = 0;           // `b` is an integer which is constant. Its value is set to zero
int * c = &a;              // `c` is a pointer to an int. `c` is pointing to `a`.
int * const d= &a;         // `d` is a constant pointer to an int. It is pointing to `a`.
const int * e = &a;        // `e` is a pointer to an int which is constant. It is pointing to `a`.
int * const f = &a;        // `f` is a constant pointer to an int. It is pointing to `a`.
const int * const g = &b;  // `g` is a constant pointer to a constant integer. It is pointing to `b`.

But what is about this:

int const * h;

Is h a pointer to a constant int? If yes, what is the different to e?

And what is about this:

int const i;

Is i this the same type as b?

eDeviser
  • 1,605
  • 2
  • 17
  • 44

1 Answers1

2

The const qualifier can be freely placed before or after a type keyword as long as there are no * symbols between them.

So the following are equivalent:

int const *h;
const int *h;

As are these:

int const i;
const int i;

But these are not:

int * const p;
const int *p;
dbush
  • 205,898
  • 23
  • 218
  • 273