Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179
Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803
Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179
Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из C\C++ эксперт
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Use reinterpret_cast to cast from char pointer to integer
#include <iostream>
using namespace std;
int main()
{
int i;
char *p = "This is a string";
i = reinterpret_cast<int> (p); // cast pointer to integer
cout << i;
return 0;
}
4448256"
Using reinterpret_cast: cast float to int
#include <cassert>
#include <iomanip>
#include <iostream>
#include <ostream>
using namespace std;
int main()
{
float pi = 1.1;
int ipi;
cout << setfill("0") << showbase << hex << internal;
assert(sizeof(int) == sizeof(float));
ipi = reinterpret_cast<int&>(pi);
cout << "pi bits=" << setw(10) << ipi << "\n";
}
pi bits=0x3f8ccccd