Phil CK

Integer to Pointer and Back

Last updated on

I was using a 3rdparty library that only provided a void* for user data. And I had a integer that I wanted to store. This is ugly but this was my solution.

#include <stddef.h>
#include <stdint.h>

id_to_ptr(uint32_t id) {
        uintptr_t to_ptr = (uintptr_t)id;
        uint32_t* ptr = to_ptr;

        ptr = (uint32_t*)warn;

        return ptr;

id_from_ptr(void *ptr) {
        size_t usr = (size_t)ptr;
        return (uint32_t)usr;