June 7, 2010

Constant pointers, pointers to constants ?

What is the difference between:
float fVar = 3.14;
const float* ptr1 = &fVar;
and:
float fVar = 3.14;
float* const ptr2 = &fVar;
? The first one is pointer to constant value, so we can't change the value of variable the pointer points to :
*ptr1 = 10;
The second one in contrary is a constant pointer to a value, and because of that we can't readdress it to point at different variable :
float fVar2;
*ptr2 = &fVar2;

No comments:

Post a Comment