/*
 * Copyright ©2026 Soham Pardeshi. All rights reserved.
 * Permission is hereby granted to students registered for University of
 * Washington CSE 333 for use solely during Summer Quarter 2026 for
 * purposes of the course. No other use, copying, distribution, or
 * modification is permitted without prior written consent. Copyrights
 * for third-party components of this work must be honored. Instructors
 * interested in reusing these course materials should contact the author.
 */

#include <iostream>  // for std::cout, std::endl
#include <cstdlib>   // for EXIT_SUCCESS

using std::cout;
using std::endl;

// const means "I promise not to modify this."  Reading is fine; writing is a
// compile error.  With pointers, const can lock the DATA, the POINTER, or both.
//
// Read a declaration right to left:
//   const int* p         -> p is a pointer to a const int   (data locked)
//   int* const p         -> p is a const pointer to an int  (pointer locked)
//   const int* const p   -> a const pointer to a const int  (both locked)

int main(int argc, char** argv) {
  int x = 5;
  const int y = 6;    // a promise: y is fixed
  // y += 1;          // ERROR: cannot modify a const value

  const int* p = &y;  // pointer to a const int: data locked, pointer free
  // *p += 1;         // ERROR: cannot write through p
  p = &x;             // OK: p itself may be re-pointed

  int* const q = &x;  // const pointer to an int: pointer locked, data free
  *q += 1;            // OK: the data is writable (x is now 6)
  // q = &y;          // ERROR: q may not be re-pointed

  const int* const r = &x;  // both locked
  // *r += 1;         // ERROR: data locked
  // r = &y;          // ERROR: pointer locked

  cout << "*p = " << *p << ", *q = " << *q << ", *r = " << *r << endl;
  return EXIT_SUCCESS;
}

// Compile with:
//   g++ -Wall -g -std=c++17 -o const const.cc