Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cast long into pointer

Tags:

c

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.

like image 258
pic11 Avatar asked Sep 18 '25 10:09

pic11


1 Answers

A more portable way (on the C99 standard variant) is to #include <stdint.h> and then cast pointers to intptr_t (and back). This integer type is guaranteed to be the size of a pointer.

like image 100
Basile Starynkevitch Avatar answered Sep 20 '25 01:09

Basile Starynkevitch