Attempting to modify a constant pointer to nonconstant data. - C++ Data Type

C++ examples for Data Type:const

Description

Attempting to modify a constant pointer to nonconstant data.

Demo Code

int main() //from  w  w  w. j a v a  2 s  .  c  om
{ 
    int x, y; 

    int * const ptr = &x; // const pointer must be initialized 

    *ptr = 7; // allowed: *ptr is not const 
    //ptr = &y; // error: ptr is const; cannot assign to it a new address 
}

Related Tutorials