fork download
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. int value = 123456789;
  6. char myChar[4]; //Integer
  7.  
  8. int *intPtr = reinterpret_cast<int*>(&myChar[0]);
  9. *intPtr = value;
  10.  
  11. int wrongValue = myChar[0]; //Only copies the first byte, converting a single char to an int.
  12. std::cout << "wrongValue: " << wrongValue << std::endl;
  13.  
  14. int rightValue = *reinterpret_cast<int*>(&myChar[0]);
  15. std::cout << "rightValue: " << rightValue << std::endl;
  16.  
  17. return 0;
  18. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
wrongValue: 21
rightValue: 123456789