const constraints on pointers

  1. int x, y;
  2. int * const ptr = &x; //ptr is a constant pointer to an integer
  3.                       //An Integer can be modified through ptr,
  4.                       //but ptr always points to the same memory
  5.                       //location.
  6. *ptr = 7;    //correct
  7. ptr = &y;    //Error

 

  1. int x=5, y;
  2. const int *const ptr = &x;  //ptr is a constant pointer to a
  3.                             //constant integer. ptr always
  4.                             //points to the same location
  5.                             //and the integer at that location
  6.                             //cannot be modified.

Guess you like

Origin blog.csdn.net/leon_founder/article/details/2782481