fork(1) download
  1. int main() {
  2. {
  3. int arrayOfInt[3] = {0, 1, 2};
  4. int * toPtr{nullptr};
  5. int ** toPtrPtr{nullptr};
  6. const int ** toPtrPtrConst{nullptr};
  7.  
  8.  
  9. toPtr = arrayOfInt;
  10.  
  11. //toPtrPtr = &arrayOfInt; //KO, I assume because of 1.
  12. //toPtrPtr = static_cast<int**>(&arrayOfInt); //KO, same as above
  13. toPtrPtr = reinterpret_cast<int**>(&arrayOfInt);
  14. toPtrPtr = (int**)&arrayOfInt;
  15.  
  16. //toPtrPtrConst = &arrayOfInt; //KO, still 1.
  17. //toPtrPtrConst = static_cast<const int**>(&arrayOfInt); //KO, still 1.
  18. toPtrPtrConst = reinterpret_cast<const int**>(&arrayOfInt); // (P1)
  19. // it is supposed to be allowed to cast int** to const int* const*, not const int**
  20. // Why is it working without requiring a const_cast to cast away the const qualifier?
  21. toPtrPtrConst = (const int**)&arrayOfInt;
  22.  
  23. //toPtrPtrConst = toPtrPtr; //KO, because of 2.
  24. //toPtrPtrConst = reinterpret_cast<const int**>(toPtrPtr); //KO because of 2.
  25. // so why is P1 allowed?
  26. }
  27.  
  28. {
  29. const int arrayOfConstInt[3] = {0, 1, 2};
  30. const int * toPtrConst{nullptr};
  31. const int ** toPtrPtrConst{nullptr};
  32. int * const * toPtrConstPtr{nullptr};
  33.  
  34. toPtrConst = arrayOfConstInt;
  35.  
  36. //toPtrPtrConst = &arrayOfConstInt; //KO, I assume because of 1.
  37. //toPtrPtrConst = static_cast<const int**>(&arrayOfConstInt); //KO, same as above
  38. //toPtrPtrConst = reinterpret_cast<const int**>(&arrayOfConstInt); // (P2)
  39. // Compiler error "casts away qualifiers", but which qualifier(s) would that cast away?
  40. toPtrPtrConst = (const int**)&arrayOfConstInt;
  41.  
  42. //toPtrConstPtr = &arrayOfConstInt; //KO, I assume because of 1.
  43. //toPtrConstPtr = static_cast<int * const *>(&arrayOfConstInt); //KO, same as above
  44. toPtrConstPtr = reinterpret_cast<int * const *>(&arrayOfConstInt); // (P3)
  45. // This one actually drops the const qualifier on the integer, but nevertheless it compiles
  46. toPtrConstPtr = (int * const *)&arrayOfConstInt;
  47.  
  48. //toPtrConstPtr = reinterpret_cast<int * const *>(&toPtrConst); // KO, because it casts away cost qualifier
  49. // so why is P3 allowed?
  50. }
  51. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
Standard output is empty