Greg M
Greg M

Reputation: 954

Set pointer to int value

Why does the compiler complain when I try to set the value of a pointer to an int? On my system, an int is the same size as a pointer. The int I have contains an address I want to set the pointer to.

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

Why isn't this possible without casting addr? Is the compiler trying to prevent me from shooting myself in the foot?

Upvotes: 2

Views: 99

Answers (1)

Sebastian Hoffmann
Sebastian Hoffmann

Reputation: 2914

Because an int is not a std::uint8_t*. C++ is a typesafe language.

Upvotes: 4

Related Questions