K
kid joe
Hi all,
Im writing a small library implementing a few standard data-structures.
For generality Im giving each node struct a "void* data" member so that
the user of the library can make a tree or list or whatever of any
data-type s/he wants.
One common thing to store is numeric types. Its very longwinded to
have to do
int *i = malloc(sizeof(*i));
*i = 42;
insert_node(X, i);
especially when there are lots of numbers to insert. On my system, its
possible to convert pointers to integers, so I can simplify this code by
just doing
insert_node(X, (void*) 42);
and then I can retrieve the data later by casting to intptr_t.
Also for unsigned integers, I can convert back to uintptr_t.
Fine so far...
What I was wondering was about floating-point types. I'd like to know
which of them (float/double/long double) has the same size as a pointer so
that I can get the conversion right... but unfortunately theres no
fltptr_t! Also using conditional compilation doesnt work because the
preprocessor doesnt seem to know about sizeof()!
Does anyone have any ideas for a good way to do this? I dont mind if the
answers only work on common desktop platforms.
Cheers,
Joe
Im writing a small library implementing a few standard data-structures.
For generality Im giving each node struct a "void* data" member so that
the user of the library can make a tree or list or whatever of any
data-type s/he wants.
One common thing to store is numeric types. Its very longwinded to
have to do
int *i = malloc(sizeof(*i));
*i = 42;
insert_node(X, i);
especially when there are lots of numbers to insert. On my system, its
possible to convert pointers to integers, so I can simplify this code by
just doing
insert_node(X, (void*) 42);
and then I can retrieve the data later by casting to intptr_t.
Also for unsigned integers, I can convert back to uintptr_t.
Fine so far...
What I was wondering was about floating-point types. I'd like to know
which of them (float/double/long double) has the same size as a pointer so
that I can get the conversion right... but unfortunately theres no
fltptr_t! Also using conditional compilation doesnt work because the
preprocessor doesnt seem to know about sizeof()!
Does anyone have any ideas for a good way to do this? I dont mind if the
answers only work on common desktop platforms.
Cheers,
Joe