fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main() {
  5. int a=11;
  6. int *pa=&a;
  7. int *root = new int{2703};
  8.  
  9. const int* const *ptr; // I want a pointer to a pointer that tshall not change
  10.  
  11. ptr = &root; // I can now access root poitner via *ptr and integer via **ptr bu I'm not allowed to change them
  12. //*ptr = &a; // not allowed, I would change root !!
  13. //**ptr=12; // not allowed either
  14. cout << **ptr<<endl; // no pb: I print the integer
  15. cout << *ptr<<endl; // no pb: I print
  16. ptr = &pa; // the pointer itself is not const
  17. cout << **ptr<<endl; // no pb: I print the integer
  18.  
  19. const int b=0; // I'm not allowed to change b
  20. const int *pb=&b; // I'm allowed to change pb but not *pb
  21. const int * const cpb = &b; // I'm not allowed to change cpb nor *cpb
  22. ptr = &cpb; // that's ok ! because ptr encforces everything that cpb enforces.
  23.  
  24.  
  25. const int** test; // this is a pointer to a pointer to a const int:
  26. //test = &cpb; // not allowed because because constness would be broken
  27. test = &pb; // allowed
  28. *test = nullptr; // but also allowed: the pointer pb is now null
  29.  
  30. return 0;
  31. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
2703
0x80f2a10
11