While reading tutorials regarding pointers, i faced a code like this
const int *p;
and
int const *p;
and
int *const p;
Whats the main difference in them?
While i assigned any integer value’s address,like
int b=100;
b=&p;
i am getting errors. Whats the point in this? Can anybody explain it with example?
Most of the time, simply read it from right to left.
“
pis a pointer to aconstint“.“
pis aconstpointer to anint“.The exception is when
constis the lefmost keyword:Then it’s the same as:
If the pointer is
const, then you can’t modify its address, but you can change the value pointed by it. If the pointed to value isconst(anintin your case), then you can’t modify what’s pointed to by the pointer (but you can make it point to something else).As an unrelated thing, this fails to compile:
Because you’re assigning the address of the pointer veriable (a
int **) tob. It’s an illegal conversion. You could force it with a cast, but I don’t think that’s what you want.