reinterpret_cast learning

reinterpret_cast
forced type conversion in c ++

usage:

reinterpret_cast<类型>(变量)

Note (the following content may run differently in different compilers)

#include<iostream>

using namespace std;

int main()
{
	int intarray[5]={1,2,3,4,5};
		
	int *intptr=intarray;
	
	cout<<"cout:  "<<'\n';
	for(int i=0;i<5;i++)
	{
		cout<<*intptr<<" at "<<reinterpret_cast<unsigned long>(intptr) <<'\n';
		intptr++;
	}
	return 0;
 } 

The examples of online courses are as above, there are some changes! But the focus remains the same,
but I reported an error when running in devc ++, as follows

[Error] cast from 'int*' to 'long unsigned int' loses precision [-fpermissive]

It is estimated that the possible reason is that the number of bytes is different, and the
int * can not be forcedly converted . It may be 8 bytes, and 4 after conversion, so the precision will be lost

So try the following instead

reinterpret_cast<unsigned long long>(intptr)

success! ! !

输出:
cout:
1 at 7339504
2 at 7339508
3 at 7339512
4 at 7339516
5 at 7339520

Published 29 original articles · Likes0 · Visits 485

Guess you like

Origin blog.csdn.net/qq_43771959/article/details/104480453