include
using namespace std;
void Test()
{
int n = 10;
(void*)n;
}
int main()
{
Test();
return 0;
}
此时使用g++编译报错:
root@ubuntu:/home/Test/Temp# g++ -o app temp.cpp
temp.cpp: In function ‘void Test()’:
temp.cpp:9:9: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
(void*)n;
将代码改为如下即可编译成功:
include
using namespace std;
void Test()
{
int n = 10;
(void*)(long)n;
}
int main()
{
Test();
return 0;
}
(void*)和long类型都是8字节,int是4字节,所以产生以上错误。
cout << sizeof(int) << endl;
cout << sizeof(long) << endl;
cout << sizeof(void*) << endl;