Can this cast fail and when?
long x=-1;
long y = (long)(void*)x;
assert(x==y);
More specifically, how to detect if the above cast is OK at compile time.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A more portable way (on the C99 standard variant) is to
#include <stdint.h>and then cast pointers tointptr_t(and back). This integer type is guaranteed to be the size of a pointer.