error: cast from 'void*' to 'int' loses precision

Joshua D. Boyd picture Joshua D. Boyd · Oct 28, 2009 · Viewed 144k times · Source

I have a function with prototype void* myFcn(void* arg) which is used as the starting point for a pthread. I need to convert the argument to an int for later use:

int x = (int)arg;

The compiler (GCC version 4.2.4) returns the error:

file.cpp:233: error: cast from 'void*' to 'int' loses precision

What is the proper way to cast this?

Answer

Ferruccio picture Ferruccio · Oct 28, 2009

You can cast it to an intptr_t type. It's an int type guaranteed to be big enough to contain a pointer. Use #include <cstdint> to define it.