简体   繁体   English

将指针设置为int值

[英]Set pointer to int value

Why does the compiler complain when I try to set the value of a pointer to an int ? 当我尝试将指针的值设置为int时,为什么编译器会抱怨? On my system, an int is the same size as a pointer. 在我的系统上, int与指针的大小相同。 The int I have contains an address I want to set the pointer to. 我拥有的int包含我要设置指针指向的地址。

int addr = 0xffff;
std::uint8_t* ptr = addr;

Why isn't this possible without casting addr ? 为什么不使用addr无法实现? Is the compiler trying to prevent me from shooting myself in the foot? 编译器是否试图阻止我用脚射击自己?

Because an int is not a std::uint8_t* . 因为int不是std::uint8_t* C++ is a typesafe language. C ++是一种类型安全的语言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM